Padding number with leading zeros in XSLT 1.0

Our Man in Bananas picture Our Man in Bananas · Sep 4, 2014 · Viewed 22.9k times · Source

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?

Answer

Tomalak picture Tomalak · Sep 4, 2014

I need to pad this with leading zeros to a length of 15 in the output (

That would be

substring(
  concat('000000000000000', msg:BankAccount/msg:Counter), 
  string-length(msg:BankAccount/msg:Counter) + 1, 
  15
)