You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT中format-number函数失效?如何格式化求和结果?

解决XSLT format-number不生效的问题

首先,你的代码里根本没有调用format-number函数,所以直接输出了原始的累加数值。要实现你需要的格式化效果,按以下步骤修改:

1. 定义欧洲风格的数字格式

在XSLT样式表的根节点(xsl:stylesheet或xsl:transform)下添加自定义decimal-format,适配你要的千分符(.)和小数点(,):

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

2. 修改累加模板的输出逻辑

在sumScorest模板的<xsl:otherwise>块中,用format-number格式化最终累加值:

<xsl:otherwise>
  <xsl:value-of select="format-number($pAccumt, '#.###,00', 'european')"/>
</xsl:otherwise>

格式模式#.###,00的含义:

  • #.###:整数部分每三位用.作为分隔符
  • ,00:强制保留两位小数,用,作为小数点

3. 修复递归终止的笔误

你的递归模板存在语法错误:$pListt[position()1]应改为$pListt[position() > 1],否则递归无法正确终止,会导致累加结果异常:

<xsl:with-param name="pListt" select="$pListt[position() > 1]"/>

完整修改后的模板代码

<xsl:template name="sumScorest">
  <xsl:param name="pListt"/>
  <xsl:param name="pAccumt" select="0"/>
  <xsl:choose>
    <xsl:when test="$pListt">
      <xsl:variable name="vHeadt" select="$pListt[1]"/>
      <xsl:call-template name="sumScorest">
        <xsl:with-param name="pListt" select="$pListt[position() > 1]"/>
        <xsl:with-param name="pAccumt" select="$pAccumt + number(substring-before(substring-after($vHeadt,'*'),'#'))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="format-number($pAccumt, '#.###,00', 'european')"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

修改后,输出数值会自动格式化为你需要的样式(比如将8795.885558844877转换为8.795,89,并自动四舍五入到两位小数)。

内容的提问来源于stack exchange,提问作者ozsubasi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 04:17:05