Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

xml - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...