XSLT 1.0 how to sum values with commas using sum() - walking in xpath

若如初见. 提交于 2021-02-05 09:13:31

问题


I have an XSLT 1.0 transformation to write and I am not finding online good solutions for my problem. I have the following XML example:

    <?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>

As you can see the sum function would not work due to the comma. What I need is to sum all the Amount values, i.e. 1,23 + 4,56 + etc...

I tried various solutions without any success. Mostly I found basic examples but nothing like this case.

The problem is that I would like a transformation to call from the XSLT code, something like:

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

So that, it will sum all the value of "Amount" in the /MainDoc/node1/node2 path.

Any help is appreciated


回答1:


I would suggest you do it this way:

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>

Applied to your example, the result will be:

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

Note that the result uses a decimal point, not decimal comma. If you want, you can change this by changing the format used by format-number().




回答2:


You can do this with a recursive template and no extension calls:

<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>

output:

5.789999999999999

If you need this rounded, you could do something like <xsl:value-of select="round($newTotal * 100) div 100" /> for that last portion there instead.



来源:https://stackoverflow.com/questions/34883703/xslt-1-0-how-to-sum-values-with-commas-using-sum-walking-in-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!