首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT货币转换

XSLT货币转换
EN

Stack Overflow用户
提问于 2011-11-21 10:54:13
回答 3查看 5.7K关注 0票数 1

我需要XSLT转换来赚钱。

在输入的XML数据中,价格是数字:

代码语言:javascript
复制
<prices>
    <price>1234</price>
    <price>1234.5</price>
</prices>

在XSLT转换之后,我需要输出XML如下(捷克格式):

代码语言:javascript
复制
<prices>
    <price>1 234,-</price>
    <price>1 234,50</price>
</prices>

可以使用XSLT完成吗?非常感谢。

编辑:,我正在使用XSLT2.0。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-23 14:25:10

此转换(在XSLT的两个版本中):

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

<xsl:decimal-format
    name="Cz1"
    grouping-separator=" "
    decimal-separator="_"/>
<xsl:decimal-format
    name="Cz2"
    grouping-separator=" "
    decimal-separator=","/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="price/text()">
      <xsl:value-of select="format-number(., '# ##0,00', 'Cz2')"/>
  </xsl:template>

 <xsl:template match="price/text()[. = floor(.)]" priority="3">
      <xsl:value-of select="format-number(., '# ##0', 'Cz1')"/>
      <xsl:text>,-</xsl:text>
  </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用时

代码语言:javascript
复制
<prices>
    <price>1234</price>
    <price>1234.5</price>
</prices>

生成想要的、正确的结果

代码语言:javascript
复制
<prices>
   <price>1 234,-</price>
   <price>1 234,50</price>
</prices>
票数 4
EN

Stack Overflow用户

发布于 2011-11-21 10:58:53

如果样式表有NumberFormat对象的实例,则可以将数字呈现为特定于地区的money String。您可以使用java.text.NumberFormat.getInstance(Locale locale).getCurrencyInstance()获得一个。然后将其添加到样式表中,然后离开。

票数 0
EN

Stack Overflow用户

发布于 2011-11-21 11:45:03

使用http://www.w3.org/TR/xslt20/#format-number,如

代码语言:javascript
复制
  <xsl:decimal-format
    name="f1"
    grouping-separator=" "
    decimal-separator=","/>

  <xsl:template match="price">
    <xsl:copy>
      <xsl:value-of select="format-number(., '# ###0,00', 'f1')"/>
    </xsl:copy>
  </xsl:template>

不过,我不知道怎样才能得到那种",-“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8210617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档