2017-08-30 55 views
-3

这看起来像是一个重复的问题,但它不是(我已经搜索过所有的SO,无法找到答案)。属性保持长度值相同的XSLT增量值

所以我有一个XSLT需要转换成XML,基本上有一个属性,其中的值必须增加,但是我们已经给出了一个设置格式的值,其中最后一部分递增,但长度该值不能超过18个字符,继承人什么,我有一个工作例如:

XSLT:

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

OUTPUT:

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

所需的输出(外部ID的长度应保持不变):

<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) --> 

如何保留值的长度不变,而增加的价值?

+0

所需的逻辑不完全清楚。你已经有15个字符;一旦你达到999,必须丢弃一些东西。 –

回答

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>