【问题标题】:XSLT 1.0 how to sum values with commas using sum() - walking in xpathXSLT 1.0 如何使用 sum() 用逗号对值求和 - 在 xpath 中行走
【发布时间】:2016-01-19 17:52:05
【问题描述】:

我有一个 XSLT 1.0 转换要编写,但我没有为我的问题找到在线好的解决方案。我有以下 XML 示例:

    <?xml version="1.0" encoding="utf-8"?>

    <MainDoc>
    <node1>
            <node2>
                    <User>jsmith</User>
                    <Amount>1,23</Amount>
            </node2>
            <node2>
                    <User>abrown</User>
                    <Amount>4,56</Amount>
            </node2>
    </node1>

正如您所见,sum 函数由于逗号而无法工作。我需要的是对所有的 Amount 值求和,即 1,23 + 4,56 + 等......

我尝试了各种解决方案,但均未成功。大多数情况下,我找到了基本示例,但没有像这种情况那样。

问题是我想从 XSLT 代码调用转换,例如:

    <xsl:call-template name="sumAll">
        <xsl:with-param name="node" select="/MainDoc/node1/node2/Amount"/> 
    </xsl:call-template>

这样,它将汇总 /MainDoc/node1/node2 路径中“Amount”的所有值。

感谢任何帮助

【问题讨论】:

标签: xml xslt xpath


【解决方案1】:

我建议你这样做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/MainDoc">
    <!-- first-pass -->
    <xsl:variable name="numbers">
        <xsl:for-each select="node1/node2/Amount">
            <num>
                <xsl:value-of select="translate(., ',', '.')"/>
            </num>
        </xsl:for-each>
    </xsl:variable>
    <!-- output -->
    <total>
        <xsl:value-of select="format-number(sum(exsl:node-set($numbers)/num), '0.00')"/>
    </total>
</xsl:template>

</xsl:stylesheet>

应用于您的示例,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<total>5.79</total>

请注意,结果使用小数点,而不是小数逗号。如果需要,您可以通过更改 format-number() 使用的格式来更改此设置。

【讨论】:

    【解决方案2】:

    您可以使用递归模板而不是扩展调用来做到这一点:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
      <xsl:template match="/">
        <xsl:call-template name="addAmount" />
      </xsl:template>
      <xsl:template name="addAmount">
        <xsl:param name="index" select="1" />
        <xsl:param name="lastVal" select="0" />
        <xsl:param name="total" select="count(//node2/Amount)" />
    
        <xsl:variable name="newTotal" select="number(translate(//node2[$index]/Amount, ',','.')) + $lastVal" />
        <xsl:if test="not($index = $total)">
          <xsl:call-template name="addAmount">
             <xsl:with-param name="index" select="$index + 1" />
             <xsl:with-param name="lastVal" select="$newTotal" />
          </xsl:call-template>
        </xsl:if>
    
        <xsl:if test="$index = $total">
          <xsl:value-of select="$newTotal" />
        </xsl:if>
    
        </xsl:template>
      <xsl:template match="text()" />
    </xsl:stylesheet>
    

    输出:

    5.789999999999999
    

    如果您需要这个四舍五入,您可以对最后一部分执行类似&lt;xsl:value-of select="round($newTotal * 100) div 100" /&gt; 的操作。

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 2017-03-13
      • 1970-01-01
      • 2016-01-25
      • 2010-10-14
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2011-01-01
      相关资源
      最近更新 更多