xslt-1.0 - XSLT 1.0 金额字段必须至少有 2 位小数

标签 xslt-1.0

我有两个 xml。有一个金额字段,可以包含 54.2,54.23,54.234,54.234567 等值。

有人请告诉我如何确保输出 xml 中至少出现两位小数。当前 54.2 转换为 54,2 ,但我希望它为 54,20

最佳答案

您可以使用 format-number() 函数将数字转换为给定格式的字符串。 如果使用“#.00##########”格式字符串,则至少会出现两位小数。

如果您有一个 xml 文件,其中可以包含 54.2,54.23,54.234,54.234567 等值:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
        <price>54.2</price>
        <price>54.23</price>
        <price>54.234</price>
        <price>54.234567</price>
</catalog>

您可以使用像这样的 xslt 转换数字以获得至少两位小数

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
      <xsl:for-each select="/catalog/price">
             <xsl:value-of select='format-number(., "#.00##########")'/><br />
      </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

然后输出:

54.20
54.23
54.234
54.234567

关于xslt-1.0 - XSLT 1.0 金额字段必须至少有 2 位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578452/

相关文章:

xml - 如何在 XSLT 中对元素进行分组

xml - XSL-T 将输入 xml 中缺少的架构元素添加为空标签

XSLT:有效测试 'cousin' 节点是否存在

xslt - 列出 XML 文件中的每个节点

XSLT 身份转换而不更改输出

xml - 在 XSLT 中创建 map

xslt - 使用xslt获取X位置的节点值

xml - 无法在 xslt 中使用 for every 提取唯一值

xml - 除 for-each 之外,哪些 XSLT 指令会更改当前节点?

xml - XSLT 1.0 (xsltproc) - 无法解析巨大的 XML