XSLT增量值的属性保持长度值相同

时间:2017-08-30 15:52:24

标签: xml xslt auto-increment

这似乎是一个重复的问题,但它不是(我搜索了所有的SO,但找不到答案)。

所以我有一个需要转换为XML的XSLT,实际上有一个属性值必须增加,但是我们已经给出了一个值的设置格式,其中最后一个部分递增但是长度为价值不能超过18个字符,这是我所拥有的一个实例:

XSLT:

<xsl:for-each select="Transactions/Transaction">
<Interaction
SourceCode="SRC12799"
ExternalID="ERHYDM000000000{position()}">
 </Interaction>
</xsl:for-each>

输出:

<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000001">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000002">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000003">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000004">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000005">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000006">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000007">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000008">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000009">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM00000000010"> <!-- Issue with length -->

期望的输出(ExternalID的长度应保持不变):

<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000001">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000002">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000003">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000004">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000005">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000006">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000007">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000008">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000009">
<Interaction SourceCode="SRC12799" ExternalID="ERHYDM0000000010"> <!-- This is the corrected  part (Same will happen for 100s 10000s and so on) -->

如何在增加值时保持值的长度不变?

1 个答案:

答案 0 :(得分:1)

以下代码将提供所需的格式。它可以从001999,即999 <Transaction>个节点。

<xsl:variable name="srcCd" select="'SRC12799'" />
<xsl:variable name="extIdPfx" select="'ERHYDM000000000'" />
<xsl:for-each select="Transactions/Transaction">
    <xsl:variable name="extId" select="concat($extIdPfx, format-number(position(),'000'))" />
    <Interaction>
        <xsl:attribute name="SourceCode">
            <xsl:value-of select="$srcCd" />
        </xsl:attribute>
        <xsl:attribute name="ExternalID">
            <xsl:value-of select="$extId" />
        </xsl:attribute>
    </Interaction>
</xsl:for-each>