xml 带逗号的 XSLT 格式编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3758978/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:18:00  来源:igfitidea点击:

XSLT format-number with comma

xmlxsltformat

提问by Daniel Falkner

I'm trying to format this and other elements alike, so it would look like this 2,590:

我正在尝试格式化这个元素和其他元素,所以它看起来像这样 2,590:

<Add_Amount>2,59</Add_Amount>

Doing it like this:

这样做:

<xsl:decimal-format name="dkk" decimal-separator="," grouping-separator="."/>

....

....

    <xsl:value-of select="translate(format-number(Add_Amount, '#.###,000', 'dkk'), ',', '.')" />

And the output comes out NaN. Any help is greatly appreciated.

输出结果为 NaN。任何帮助是极大的赞赏。

Thanks.

谢谢。

//Daniel

//丹尼尔

回答by Dimitre Novatchev

Use:

使用

format-number(translate(., ',','.'), '#.###,000', 'd')

This transformation:

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:decimal-format name="d"
  decimal-separator="," grouping-separator="."/>

 <xsl:template match="/">
   <xsl:value-of select=
   "format-number(translate(., ',','.'), '#.###,000', 'd')"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

当应用于提供的 XML 文档时

<Add_Amount>2,59</Add_Amount>

produces the wanted result:

产生想要的结果

2,590

The problem with your code isthat 2,59isn't a valid number and must be converted to such, before passing this as the first argument of format-number().

您的代码的问题在于2,59它不是有效数字,必须先转换为有效数字,然后再将其作为format-number().

回答by Bart Kummel

The extra call to translate()in Dimitre Novatchev's answer seems unnecessary. The use of <xsl:decimal-format>should be enough, like this:

translate()Dimitre Novatchev 的回答中的额外呼叫似乎没有必要。使用<xsl:decimal-format>应该就够了,像这样:

<xsl:decimal-format name="euroFormat" decimal-separator="," grouping-separator="."/>
<xsl:value-of select="format-number(text(), '###.###,00', 'euroFormat')"/>

回答by Ashish Kumar

You can simply do this like:

你可以简单地这样做:

<xsl:value-of select="format-number(number, '#,###')"/>