Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding number with leading zeros in XSLT 1.0

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?

like image 791
Our Man in Bananas Avatar asked Sep 04 '14 09:09

Our Man in Bananas


People also ask

How can I pad a value with leading zeros?

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.

What is Number () in XSLT?

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.

Which symbol is used to get the element value in XSLT?

XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.

How to pad a numeric value with leading zeros?

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.

How to add leading zeros to the length of a string?

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?

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.

What is the syntax of XSLT format-number?

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


2 Answers

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

like image 129
Daniel Haley Avatar answered Sep 19 '22 16:09

Daniel Haley


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')" />
like image 31
payas Avatar answered Sep 20 '22 16:09

payas