0

我是 XSLT 的新手,我正在尝试将我的客户端 xml 数据转换为固定宽度的文件。有一个条件是所有行(最后一行除外)都必须以回车符结尾。我不知道如何在最后一行之后摆脱那个回车。这是我到目前为止所拥有的:-->

    <xsl:for-each select="/wd:Report_Data/wd:Report_Entry">
        <xsl:value-of select="substring(concat('0',wd:XMLNAME_2,$padding),1,2)"/>

        <xsl:value-of select="substring(concat(wd:Company/wd:Employer_s_Federal_ID_Number,$padding),1,9)"/>

        <xsl:value-of select="substring(concat((wd:Company_-_Name),$padding),1,45)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Address_-_Formatted_Line_1,$padding),1,40)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Address_-_Formatted_Line_2,$padding),1,40)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Address_-_Formatted_Line_3,$padding),1,40)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:city,$padding),1,25)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:State_ISO_Code,$padding),1,2)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Postal_Code,$padding),1,5)"/>

        <xsl:value-of select="substring(concat(' ',$padding),1,4)"/>

        <xsl:value-of select="substring(concat(wd:CF_NewHire_Contact_First,$padding),1,16)"/>

        <xsl:value-of select="substring(concat(wd:CF_NewHire_Contact_Last,$padding),1,30)"/>

        <xsl:value-of select="substring(concat(CF_NewHire_Contact_Ext,CF_NewHIre_Contact_Phone,$padding),1,10)"/>

        <xsl:value-of select="substring(concat(wd:Social_Security_Number,$padding),1,9)"/>

        <xsl:value-of select="substring(concat(wd:Legal_Name_-_First_Name,$padding),1,16)"/>

        <xsl:value-of select="substring(concat(wd:Legal_Name_-_Middle_Name,$padding),1,6)"/>

        <xsl:value-of select="substring(concat(wd:Legal_Name_-_Last_Name,$padding),1,30)"/>

        <xsl:value-of select="substring(concat(wd:CF_Global_BirthDate_yyyymmdd,$padding),1,8)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:State_ISO_Code_2,$padding),1,2)"/>

        <xsl:value-of select="substring(concat(wd:CF_Global_Hire_yyyymmdd,$padding),1,8)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Address_-_Formatted_Line_1,$padding),1,40)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Address_-_Formatted_Line_2,$padding),1,40)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Address_-_Formatted_Line_3,$padding),1,40)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:city,$padding),1,25)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:State_ISO_Code,$padding),1,2)"/>

        <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Postal_Code,$padding),1,5)"/>

        <xsl:value-of select="substring(concat(' ',$padding),1,4)"/>

        <xsl:value-of select="substring(concat(' ',$padding),1,35)"/>

        <xsl:value-of select="substring(concat(' ',$padding),1,35)"/>
        <xsl:value-of select="$delimiter"></xsl:value-of>
    </xsl:for-each> 
</xsl:template>        

4

2 回答 2

1

代替

<xsl:value-of select="$delimiter"></xsl:value-of>

<xsl:if test="position() != last()">
  <xsl:value-of select="$delimiter"></xsl:value-of>
</xsl:if>
于 2013-09-17T17:09:47.163 回答
0

您可以在 XSLT 2.0(如果您使用 2.0)中使用 last() 命令和如下测试:

<xsl:if test="position() &lt; /wd:Report_Data/wd:Report_Entry[last()]">
    <xsl:value-of select="$delimiter"/>
</xsl:if>

这是我的 XSLT 的完整示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="uri">
    <xsl:output method="text" />

    <xsl:variable name="padding">
        <xsl:text>_</xsl:text>
    </xsl:variable>

    <xsl:variable name="delimiter">
        <xsl:text>|</xsl:text>
    </xsl:variable>

    <xsl:template match="/wd:root">
        <xsl:for-each select="wd:Report_Data/wd:Report_Entry">
            <xsl:value-of select="substring(concat('0',wd:XMLNAME_2,$padding),1,2)"/>
            <xsl:value-of select="substring(concat(wd:Company/wd:Employer_s_Federal_ID_Number,$padding),1,9)"/>
            <xsl:value-of select="substring(concat((wd:Company_-_Name),$padding),1,45)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Address_-_Formatted_Line_1,$padding),1,40)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Address_-_Formatted_Line_2,$padding),1,40)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Address_-_Formatted_Line_3,$padding),1,40)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:city,$padding),1,25)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:State_ISO_Code,$padding),1,2)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:Postal_Code,$padding),1,5)"/>
            <xsl:value-of select="substring(concat(' ',$padding),1,4)"/>
            <xsl:value-of select="substring(concat(wd:CF_NewHire_Contact_First,$padding),1,16)"/>
            <xsl:value-of select="substring(concat(wd:CF_NewHire_Contact_Last,$padding),1,30)"/>
            <xsl:value-of select="substring(concat(CF_NewHire_Contact_Ext,CF_NewHIre_Contact_Phone,$padding),1,10)"/>
            <xsl:value-of select="substring(concat(wd:Social_Security_Number,$padding),1,9)"/>
            <xsl:value-of select="substring(concat(wd:Legal_Name_-_First_Name,$padding),1,16)"/>
            <xsl:value-of select="substring(concat(wd:Legal_Name_-_Middle_Name,$padding),1,6)"/>
            <xsl:value-of select="substring(concat(wd:Legal_Name_-_Last_Name,$padding),1,30)"/>
            <xsl:value-of select="substring(concat(wd:CF_Global_BirthDate_yyyymmdd,$padding),1,8)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Work_Address/wd:State_ISO_Code_2,$padding),1,2)"/>
            <xsl:value-of select="substring(concat(wd:CF_Global_Hire_yyyymmdd,$padding),1,8)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Address_-_Formatted_Line_1,$padding),1,40)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Address_-_Formatted_Line_2,$padding),1,40)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Address_-_Formatted_Line_3,$padding),1,40)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:city,$padding),1,25)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:State_ISO_Code,$padding),1,2)"/>
            <xsl:value-of select="substring(concat(wd:Primary_Home_Address/wd:Postal_Code,$padding),1,5)"/>
            <xsl:value-of select="substring(concat(' ',$padding),1,4)"/>
            <xsl:value-of select="substring(concat(' ',$padding),1,35)"/>
            <xsl:value-of select="substring(concat(' ',$padding),1,35)"/>
            <xsl:if test="position() &lt; /wd:root/wd:Report_Data/wd:Report_Entry[last()]">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

应用于此 XML 时:

<?xml version="1.0" encoding="UTF-8"?>
<wd:root xmlns:wd="uri">
    <wd:Report_Data>
        <wd:Report_Entry>
            <wd:Legal_Name_-_First_Name>Mark</wd:Legal_Name_-_First_Name>
            <wd:Legal_Name_-_Last_Name>Veenstra</wd:Legal_Name_-_Last_Name>
        </wd:Report_Entry>
        <wd:Report_Entry>
            <wd:Legal_Name_-_First_Name>Veenstra</wd:Legal_Name_-_First_Name>
            <wd:Legal_Name_-_Last_Name>Mark</wd:Legal_Name_-_Last_Name>
        </wd:Report_Entry>
        <wd:Report_Entry>
            <wd:Legal_Name_-_First_Name>M</wd:Legal_Name_-_First_Name>
            <wd:Legal_Name_-_Last_Name>Veenstra</wd:Legal_Name_-_Last_Name>
        </wd:Report_Entry>
        <wd:Report_Entry>
            <wd:Legal_Name_-_First_Name>Veenstra</wd:Legal_Name_-_First_Name>
            <wd:Legal_Name_-_Last_Name>M</wd:Legal_Name_-_Last_Name>
        </wd:Report_Entry>
    </wd:Report_Data>
</wd:root>

它产生一个没有最后一个分隔符的 ASCII。

于 2013-09-17T17:09:24.873 回答