XSLT格式编号,从最右边的数字开始每隔4个数字插入“-" [英] XSLT formatting numbers, insert '-' after every 4 digits starting from the right most digit
问题描述
我有一个27位长的数字,出于可读性考虑,我需要设置以下格式:
I have a number that's 27 digits long, I need to format for readability purposes as below :
Input : 999967799857791961574987365
Expected output : 999-9677-9985-7791-9615-7498-7365
因此, 从最右边开始,我需要在每4位数字后插入一个'-' .
So in words, Starting from the right most, I need to insert a '-' after every 4 digits.
我一直在使用
<xsl:value-of select="format-number($ID, '###-####-####-####-####-####-####-####')" />
但它根本不起作用.
任何帮助或指针都将很好..欢呼
Any help or pointers would be great..Cheers
推荐答案
不能像现在那样传递格式字符串并使其起作用的原因是,格式字符串中的字符都具有特殊的意义. -"被理解为减号,而不是分组分隔符.要使其工作,您必须使用xsl:decimal-format定义格式,并使用format-number的可选第三个参数引用它.这是一个完整的样式表,您可以调用任何XML文件进行说明:
The reason you can't just pass in the format string as you did and have it work is that the characters in the format string all have a special significance. The "-" is understood as the minus sign, not as a grouping separator. To make it work, you have to define a format using xsl:decimal-format and reference it using the optional third argument to format-number. Here's a complete stylesheet you can call on any XML file to illustrate:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:decimal-format name="dashes" grouping-separator="-"/>
<xsl:template match="/">
<xsl:value-of select="format-number(999967799857791961574987365,
'-####', 'dashes')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT格式编号,从最右边的数字开始每隔4个数字插入“-"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!