xml XSLT 1.0 中带前导零的填充数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25662151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Padding number with leading zeros in XSLT 1.0
提问by Our Man in Bananas
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.
我们在 XML 中有一个数字,在一个大的 XML 文件中最多可以有 3 位数字,必须将其转换为固定长度的文本才能加载到另一个系统中。
I need to pad this with leading zeros to a length of 15 in the output (which is fixed length text)
我需要在输出中用前导零填充它的长度为 15(这是固定长度的文本)
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
在开头获取 15 个零并取子字符串,但我一定对子字符串犯了错误,因为在我得到的结果中
0000000000000000000000009LLOYDS BANK PLC
00000000000000000000000010LLOYDS BANK PLC
I also tried format-numberbut I it returns NaN
我也试过,format-number但它返回 NaN
<xsl:value-of select="format-number(msg:BankAccount/msg:Counter, '000000000000000')"/>
returns 'NaN'
返回'NaN'
so what have I done wrong and what is the best way to do this?
那么我做错了什么,最好的方法是什么?
回答by Tomalak
I need to pad this with leading zeros to a length of 15 in the output (
我需要在输出中用前导零填充它的长度为 15(
That would be
那将是
substring(
concat('000000000000000', msg:BankAccount/msg:Counter),
string-length(msg:BankAccount/msg:Counter) + 1,
15
)
回答by Michael Kay
Another approach is
另一种方法是
substring(string(1000000000000000 + $x), 2)
回答by payas
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')" />
回答by Daniel Haley
Another option is xsl:number...
另一种选择是xsl:number...
<xsl:number value="number_to_format" format="000000000000001"/>
Full Example...
完整示例...
XML Input
XML 输入
<doc>
<test>1</test>
<test>11</test>
<test>250</test>
</doc>
XSLT 1.0
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
回答by JayTeeEye
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>

