for-each循环的子串结果

时间:2013-01-23 12:04:24

标签: xslt foreach

我的XSLT中有一个for-each循环,它从输入XML中为我提供了CUST_PO_NUMBER的不同值的串联字符串

<xsl:key name="custpo"
         match="LIST_G_DELV/G_DELV/LIST_G_INV_LINES/G_INV_LINES"
         use="CUST_PO_NUMBER"/>
<xsl:template match="XXFIN_GBL_RAINV_PRINT">
    <xsl:apply-templates select="LIST_G_INV"/>
</xsl:template>
<xsl:template match="LIST_G_INV">
    <xsl:for-each select="G_INV">
        <xsl:value-of select="CUSTOMER_TRX_ID"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="substring(LIST_G_SEB/G_SEB/SEB_NAME,1,255)"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="substring(IDM_BILL_TO_CUSTOMER_NAME,1,255)"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="BILL_TO_CUSTOMER_NUMBER"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="TRX_NUMBER"/>
        <xsl:text>|</xsl:text>
        <xsl:for-each select="LIST_G_DELV/G_DELV/LIST_G_INV_LINES/G_INV_LINES[generate-id()=generate-id(key('custpo',CUST_PO_NUMBER)[1])]">
            <xsl:value-of select="CUST_PO_NUMBER"/>
            <xsl:if test="not(position() = last())">
                <xsl:text>;</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="CUST_ISO_LANGUAGE"/>
    </xsl:for-each>
</xsl:template>

此XSLT的最终输出是CSV文件(由管道分隔|)。我们可以将CUST_PO_NUMBER元素上for-each循环的结果截断为1000个字符吗?

1 个答案:

答案 0 :(得分:0)

我相信如果您替换以下for-each

,这应该有用
<xsl:for-each select="LIST_G_DELV/G_DELV/LIST_G_INV_LINES/G_INV_LINES[generate-id()=generate-id(key('custpo',CUST_PO_NUMBER)[1])]">
        <xsl:value-of select="CUST_PO_NUMBER"/>
        <xsl:if test="not(position() = last())">
            <xsl:text>;</xsl:text>
        </xsl:if>
</xsl:for-each>

使用:

 <xsl:variable name="values">
    <xsl:for-each select="LIST_G_DELV/G_DELV/LIST_G_INV_LINES/G_INV_LINES[generate-id()=generate-id(key('custpo',CUST_PO_NUMBER)[1])]">
        <xsl:value-of select="CUST_PO_NUMBER"/>
        <xsl:if test="not(position() = last())">
            <xsl:text>;</xsl:text>
        </xsl:if>
    </xsl:for-each>
 </xsl:variable>
 <xsl:value-of select="substring($values, 1, 1000)" />

并且由于您仍在使用substring(),因此您可以通过以下方式消除iftext

 <xsl:variable name="values">
    <xsl:for-each select="LIST_G_DELV/G_DELV/LIST_G_INV_LINES/G_INV_LINES[generate-id()=generate-id(key('custpo',CUST_PO_NUMBER)[1])]">
        <xsl:value-of select="concat(';', CUST_PO_NUMBER)"/>
    </xsl:for-each>
 </xsl:variable>
 <xsl:value-of select="substring($values, 2, 1001)" />