We have a number in XML that can go up to 3 digits in a large XML file that has to be converted to fixed length text for loading into another system.
I need to pad this with leading zeros to a length of 15 in the output (which is fixed length text)
Examples:
- 1 becomes 000000000000001
- 11 becomes 000000000000011
- 250 becomes 000000000000250
I tried this:
<xsl:value-of select="substring(concat('000000000000000', msg:BankAccount/msg:Counter), 12, 15)"/>
to get the 15 zeros at the beginning and take the substring but I must have made a mistake with the substring because in the results I get
0000000000000000000000009LLOYDS BANK PLC
00000000000000000000000010LLOYDS BANK PLC
I also tried format-number
but I it returns NaN
<xsl:value-of select="format-number(msg:BankAccount/msg:Counter, '000000000000000')"/>
returns 'NaN'
so what have I done wrong and what is the best way to do this?
To pad an integer with leading zeros to a specific length To display the integer as a decimal value, call its ToString(String) method, and pass the string "Dn" as the value of the format parameter, where n represents the minimum length of the string.
Definition and Usage The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.
XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.
To pad a numeric value with leading zeros to a specific length. Determine how many digits to the left of the decimal you want the string representation of the number to have. Include any leading zeros in this total number of digits. Define a custom numeric format string that uses the zero placeholder "0" to represent the minimum number of zeros.
Determine the length of the unpadded numeric string by calling the integer value's ToString ("D").Length or ToString ("X").Length method. Add the number of leading zeros that you want to include in the formatted string to the length of the unpadded numeric string. Adding the number of leading zeros defines the total length of the padded string.
Is there any function that pads zeros on the left? We don't know the coming input string length. If it is less than 20 we have to pad zeros on the left side. If the input string length is 10 then we have to pad 10 zeros in the left side.
XSLT format-number is defined to convert a number into a string value using the second argument’s pattern. This is the type conversion formatting output. We use a different notation for numbers like comma for decimal formats and some points for thousands representation. To use all these, XSL uses format-number. Syntax of XSLT format-number
Another option is xsl:number
...
<xsl:number value="number_to_format" format="000000000000001"/>
Full Example...
XML Input
<doc>
<test>1</test>
<test>11</test>
<test>250</test>
</doc>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="test">
<xsl:number value="." format="000000000000001"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output
000000000000001
000000000000011
000000000000250
Fiddle: http://xsltfiddle.liberty-development.net/pPzifpg
It can be also done using string-format
<xsl:value-of select="format-number(msg:BankAccount/msg:Counter, '000000000000000')" />
in general:
<xsl:value-of select="format-number(number_you_would_like_to_padd, 'string_how_much_zeros_you_would like')" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With