Vladson
Vladson

Reputation: 47

Sum values in for-each for XSLT 1.0 with different node names

Can you help me to sum values in for-each by using XSLT 1.0. Which contains different node names.

I have an XML:

<root>
 <object>
  <NUM-105>AAA</NUM-105>
  <NUM-105>3755.45</NUM-105>
  <NUM-60>BBB</NUM-60>
  <NUM-60>817633.21</NUM-60>
  <NUM-107>AAA</NUM-107>
  <NUM-107>200.03</NUM-107>
  <NUM-1107>CCC</NUM-1107>
  <NUM-1107>20</NUM-1107>
  <NUM-90>BBB</NUM-90>
  <NUM-90>13072806.23</NUM-90>
  <NUM-1107>AAA</NUM-1107>
  <NUM-1107>10</NUM-1107>
 </object>
</root>

And right now my XSL looking like this:

<?xml version="1.0"?>

<xsl:template match="root/object/*[starts-with(name(),'NUM-')]">
    <xsl:if test="not(number(text())) and text() != '0'">
        <xsl:variable name="numNumber">
            <xsl:value-of select="substring-after(name(),'NUM-')"/>
        </xsl:variable>
        <xsl:variable name="numName">
            <xsl:value-of select="text()"/>
        </xsl:variable>
        <xsl:if test="not(preceding-sibling::*[starts-with(name(),'NUM-')]/text() = $numName)">
            <output name="{$numName}" id="{$numNumber}">
                <xsl:for-each select="ancestor::root/object/*[text() = $numName]">
                    <xsl:variable name="sum">
                        <xsl:value-of select="number(following-sibling::node()/text())"/>
                    </xsl:variable>
                    <sum value="{$sum}"/>
                </xsl:for-each>
            </output>
        </xsl:if>
    </xsl:if>
</xsl:template>

Which is returning OUTPUT like this :

<output name="AAA" id="105">
  <sum value="3755.45"/>
  <sum value="200.03"/>
  <sum value="10"/>
</output>

<output name="BBB" id="105">
  <sum value="817633.21"/>
  <sum value="13072806.23"/>
</output>

<output name="CCC" id="105">
  <sum value="20"/>
</output>

Instead of having multiple "sum" rows, i need only one row with total of all values. So for example for "AAA" it will be : 3965.48 and etc. As you can see, you can't go through all nodes, cause they have different node name() : NUM-**** which is unique only to the name and value.

Upvotes: 0

Views: 801

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

I would prefer to do this in two steps: first, create a single node for each pair of name and number, so that we have something like:

  <amount name="AAA" value="3755.45"/>
  <amount name="BBB" value="817633.21"/>
  <amount name="AAA" value="200.03"/>
  <amount name="CCC" value="20"/>
  <amount name="BBB" value="13072806.23"/>
  <amount name="AAA" value="10"/>

The next step is to apply Muenchian grouping to the result.

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:strip-space elements="*"/>

<xsl:key name="amt-by-name" match="amount" use="@name" />

<xsl:template match="/root">
    <!-- FIRST PASS -->
    <xsl:variable name="amounts">
        <xsl:for-each select="object/*[position() mod 2 = 1]">
            <amount name="{.}" value="{following-sibling::*}"/>
        </xsl:for-each>
    </xsl:variable>
    <!-- OUTPUT -->
    <output>
        <!-- FOR EACH DISTINCT NAME -->
        <xsl:for-each select="exsl:node-set($amounts)/amount[count(. | key('amt-by-name', @name)[1]) = 1]">
            <!-- SUM CURENT GROUP -->
            <sum name="{@name}" value="{sum(key('amt-by-name', @name)/@value)}"/>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

Applied to a well-formed (!) example input:

XML

<root>
 <object>
  <NUM-1054>AAA</NUM-1054>
  <NUM-1054>3755.45</NUM-1054>
  <NUM-601>BBB</NUM-601>
  <NUM-601>817633.21</NUM-601>
  <NUM-1072>AAA</NUM-1072>
  <NUM-1072>200.03</NUM-1072>
  <NUM-11072>CCC</NUM-11072>
  <NUM-11072>20</NUM-11072>
  <NUM-900>BBB</NUM-900>
  <NUM-900>13072806.23</NUM-900>
  <NUM-11074>AAA</NUM-11074>
  <NUM-11074>10</NUM-11074>
 </object>
</root>

this will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <sum name="AAA" value="3965.48"/>
  <sum name="BBB" value="13890439.44"/>
  <sum name="CCC" value="20"/>
</output>

Upvotes: 1

Related Questions