如何创建一个XSL来输出固定位置文本文件?

时间:2018-05-14 14:10:45

标签: xml xslt xslt-1.0 xslt-2.0

请在下面找到我作为输入的XML文件示例:

<?xml version="1.0" encoding="UTF-8"?>
<DATA_DS>
<ARCHIVEACTIONID></ARCHIVEACTIONID>
<DELIVERYOPTIONID></DELIVERYOPTIONID>
<PAYROLLACTIONID></PAYROLLACTIONID>
<FLOWINSTANCENAME>pb21</FLOWINSTANCENAME>
<G_1>
    <PAYROLL_ACTION_ID>665</PAYROLL_ACTION_ID>
    <G_2>
        <FILE_FRAGMENT>
            <Person_Benefit_Extract_Custom>
                <REP_CATEGORY_NAME>Person Benefit Extract Custom</REP_CATEGORY_NAME>
                <parameters>
                    <request_id>300000163751</request_id>
                    <FLOW_NAME>pbtestrun</FLOW_NAME>
                    <legislative_data_group_id/>
                    <effective_date>2018-04-07</effective_date>
                    <start_date/>
                    <report_category_id>300000163719</report_category_id>
                    <action_parameter_group_id/>
                </parameters>
                <Benefit_Child_Data_Group>
                    <OBJECT_ACTION_ID>1074</OBJECT_ACTION_ID>
                    <Person_Benefit_Traversal_Record>
                        <Benefit_Child_Data_Group>
                            <Benefit_1_Detail_Record>
                                <Emplyee_Person_Number>12345</Emplyee_Person_Number>
                                <Employee_First_Name>John</Employee_First_Name>
                                <Employee_Last_Name>Doe</Employee_Last_Name>
                                <Parent_Dependent_Bridge_Data_Group>
                                    <Benefit_1_2_Bridge_Traversal_Record>
                                        <Benefit_2_Child_Data_Group>
                                            <Benefit_2_Detail_Record>
                                                <Parent_Employee_Number>12345</Parent_Employee_Number>
                                                <Dependent_First_Name>Spouse First Name</Dependent_First_Name>
                                                <Dependent_Last_Name>Spouse Last Name</Dependent_Last_Name>
                                                <Dependent_Plan_Name>Medical Plan</Dependent_Plan_Name>
                                            </Benefit_2_Detail_Record>
                                        </Benefit_2_Child_Data_Group>
                                        <Benefit_2_Child_Data_Group>
                                            <Benefit_2_Detail_Record>
                                                <Parent_Employee_Number>12345</Parent_Employee_Number>
                                                <Dependent_First_Name>Child First Name</Dependent_First_Name>
                                                <Dependent_Last_Name>Child Last Name</Dependent_Last_Name>
                                                <Dependent_Plan_Name>Medical Plan</Dependent_Plan_Name>
                                            </Benefit_2_Detail_Record>
                                        </Benefit_2_Child_Data_Group>
                                        <Benefit_2_Child_Data_Group>
                                            <Benefit_2_Detail_Record>
                                                <Parent_Employee_Number>12345</Parent_Employee_Number>
                                                <Dependent_First_Name>Child2 First Name</Dependent_First_Name>
                                                <Dependent_Last_Name>Child2 Last Name</Dependent_Last_Name>
                                                <Dependent_Plan_Name>Medical Plan</Dependent_Plan_Name>
                                            </Benefit_2_Detail_Record>
                                        </Benefit_2_Child_Data_Group>                                           
                                    </Benefit_1_2_Bridge_Traversal_Record>
                                </Parent_Dependent_Bridge_Data_Group>
                            </Benefit_1_Detail_Record>
                        </Benefit_Child_Data_Group>
                    </Person_Benefit_Traversal_Record>
                </Benefit_Child_Data_Group>
            </Person_Benefit_Extract_Custom>
        </FILE_FRAGMENT>
    </G_2>
</G_1>
</DATA_DS>

输出应该是固定位置文本文件,显示如下(如果存在依赖项,则打印同一行上的所有依赖项 - 如果存在员工,则从下一行开始打印):

Field                            Start Position            Length
Emplyee_Person_Number            1                         10
Employee_First_Name              11                        15
Employee_Last_Name               16                        15
Parent_Employee_Number           NA                        10  
Dependent_First_Name             NA                        15
Dependent_Last_Name              NA                        15
Dependent_Plan_Name              NA                        15

示例输出:

12345     John           Doe            12345     Spouse First NaSpouse Last NamMedical Plan   12345     Child First NamChild Last NameMedical Plan   12345     Child2 First NaChild2 Last NamMedical Plan

第二,第三......员工和各自的家属将在新行上打印。

2 个答案:

答案 0 :(得分:1)

我建议进行两阶段转换。首先使用通用标签生成一个表,比如说

<table>
  <head>
     ...
  </head>
  <row>
    <cell>..
  </row>...
</table>

然后将其转换为固定宽度的ASCII再现。你已将它标记为xslt 1.0和xslt 2.0,因此很难知道你想要的是什么。将字符串$填充到$n长度的最简单方法是使变量$spaces包含一个所需空格的字符串,然后执行substring(concat($s, $spaces), 1, $n)

答案 1 :(得分:0)

我认为有些推送编程是有序的。只要XML的结构是静态的,即使对于空值,这也可能对您有用。

<xsl:template match="node()">
  <xsl:apply-templates select="node()"/>
</xsl:template>

<xsl:template match="Benefit_1_Detail_Record">
  <xsl:apply-templates select="node()"/>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="Emplyee_Person_Number">
  <xsl:value-of select="substring(concat(.,'          '), 1, 10)"/>
</xsl:template>

<xsl:template match="Employee_First_Name">
  <xsl:value-of select="substring(concat(., '               '), 1, 15)"/>
</xsl:template>

<xsl:template match="Employee_Last_Name">
  <xsl:value-of select="substring(concat(., '               '), 1, 15)"/>
</xsl:template>

<xsl:template match="Parent_Employee_Number">
  <xsl:value-of select="substring(concat(., '          '), 1, 10)"/>
</xsl:template>

<xsl:template match="Dependent_First_Name">
  <xsl:value-of select="substring(concat(., '               '), 1, 15)"/>
</xsl:template>

<xsl:template match="Dependent_Last_Name">
  <xsl:value-of select="substring(concat(., '               '), 1, 15)"/>
</xsl:template>

<xsl:template match="Dependent_Plan_Name">
  <xsl:value-of select="substring(concat(., '               '), 1, 15)"/>
</xsl:template>