Padding number with leading zeros in XSLT 1.0
Asked Answered
C

5

12

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?

Corelation answered 4/9, 2014 at 9:41 Comment(3)
Does msg:Counter have "LLOYDS BANK PLC" at the end of it? format-number() only works on numbers.Killian
no, it only has a number, I kept in the 'LLOYDS BANK PLC' to show the effect that the error is causingCorelation
If msg:Counter is a number then there is no reason format-number should return NaN. There's something wrong that you're not telling us about.Wotan
S
17

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
)
Stadtholder answered 4/9, 2014 at 9:59 Comment(0)
W
18

Another approach is

substring(string(1000000000000000 + $x), 2)
Wotan answered 4/9, 2014 at 13:24 Comment(3)
Yes, I was assuming $x is a number, or a value that will be converted to a number (details depend on whether it's XPath 1.0 or 2.0).Wotan
Very elegant solutionNarcose
Perfect. The accepted answer didn't work for me, but this does and is more concise, too!Tyndale
S
17

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
)
Stadtholder answered 4/9, 2014 at 9:59 Comment(0)
Y
14

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')" />
Yand answered 12/9, 2016 at 14:42 Comment(0)
A
5

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>&#xA;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

Output

000000000000001
000000000000011
000000000000250

Fiddle: http://xsltfiddle.liberty-development.net/pPzifpg

Adaline answered 13/3, 2019 at 17:27 Comment(1)
I used xsl:number and it worked like a charm! 😊Melanoma
N
1

Another option to consider....

<xsl:apply-templates select="Groups/Group[@Name='TheSource']/Field[@Name='BankAccount']" />
<xsl:text>999999999999999</xsl:text>
<!-- uncomment below and comment above line to use without test number -->
<!-- <xsl:text>|</xsl:text> -->

<xsl:template match="Groups/Group[@Name='BankAcctFile']/Field[@Name='BankAccount']">
    <xsl:call-template name="padleft">
        <xsl:with-param name="padChar" select="'0'" />
        <xsl:with-param name="padVar" select="substring(current(),1,15)" />
        <xsl:with-param name="length" select="15" />
    </xsl:call-template>
</xsl:template>
Nun answered 26/9, 2019 at 21:45 Comment(3)
Without the contents of the called template, this does not provide an answer to the question.Zimbabwe
contents of a called template example added above.Nun
No, it was not.Zimbabwe

© 2022 - 2024 — McMap. All rights reserved.