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.2k views
in Technique[技术] by (71.8m points)

xml - XSLT format-number with comma

I'm trying to format this and other elements alike, so it would look like this 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.

Thanks.

//Daniel

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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:

<Add_Amount>2,59</Add_Amount>

produces the wanted result:

2,590

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


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

...