<Data>
<AA>123</AA>
<AA>45658</AA>
<AA>123456789</AA>
</Data>
Output needed:
<info>
<Numbers id="000000123" />
<Numbers id="000045658" />
<Numbers id="123456789" />
</info>
Condition:
I need to check for the string length of value of the element AA. If the length is less than 9 I need to append ‘0’ s to make it 9 digit in length.
XSLT wrote:
<xsl:template match="Data">
<info>
<xsl:for-each select="AA">
<Numbers>
<xsl:attribute name="id">
<xsl:variable name="numlenght">
<xsl:value-of select="string-length(.)" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$numlenght<9">
<xsl:variable name="balance">
<xsl:value-of select="9-$numlenght"/>
</xsl:variable>
<xsl:for-each select="//*[position() <$balance]">
<xsl:value-of select="'0'"/>
</xsl:for-each>
</xsl:when>
</xsl:choose>
<xsl:value-of select="."/>
</xsl:attribute>
</Numbers>
</xsl:for-each>
</info>
</xsl:template>
I am not getting the exact output.
Can any one suggest how to do that?
This requires less memory:
Here is a complete transformation:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
The expression:
doesn’t construct a string with length greater than 9 (as in another answer, that after this extracts a substring of the longer than 9 characters string).
In contrast, it extracts only the necessary for padding number of
0characters and then concatenates them to the current node’s string value.Do note:
This:
can be simplified further to this: