消除XSLT中最后一行的回车

时间:2013-09-17 16:31:13

标签: xml xslt

我是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>        

2 个答案:

答案 0 :(得分:1)

替换

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

<xsl:if test="position() != last()">
  <xsl:value-of select="$delimiter"></xsl:value-of>
</xsl:if>

答案 1 :(得分: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。