0

I have the below xml

   <pair>
            <name>ABC</name>
            <value>10</value>
    </pair>
    <pair>
            <name>ABC</name>
            <value>50</value>
    </pair>
    <pair>
            <name>XYZ</name>
            <value>30</value>
    </pair>

which should be coverted into

<pair>
        <name>ABC</name>
        <value>60</value>
</pair>
<pair>
        <name>XYZ</name>
        <value>30</value>
</pair>

That is if the name is same in the name value pair. The values against a same name should be added in output xml. Can anybody please help?

Usm
  • 3
  • 1
  • 1
    This is a *grouping* question - do a search, it's one of the most often asked questions here. Note that answers are different for XSLT 1.0 or 2.0. – michael.hor257k Jan 21 '15 at 12:10

2 Answers2

1

Assuming XSLT 2.0 and an XSLT 2.0 processor like Saxon 9 you can use

<xsl:template match="/*">
  <xsl:copy>
    <xsl:for-each-group select="//pair" group-by="name">
      <xsl:copy>
        <xsl:copy-of select="name"/>
        <value><xsl:value-of select="sum(current-group()/value)"/></value>
      </xsl:copy>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

XML:

<parent>
    <pair>
        <name>ABC</name>
        <value>10</value>
    </pair>
    <pair>
        <name>ABC</name>
        <value>50</value>
    </pair>
    <pair>
        <name>XYZ</name>
        <value>30</value>
    </pair>
</parent>

XSLT:

<xsl:template match="/">
        <parent>
            <xsl:for-each select="//parent/pair[not(name=preceding::pair/name)]">
                <xsl:variable name="curName" select="name" />
                <pair>
                <name>
                <xsl:value-of select="name"/>
                </name>
                <value>
                <xsl:value-of select="sum(/parent/pair[name = $curName]/value)"/>
                </value>
                </pair>
            </xsl:for-each>
        </parent>
</xsl:template>

OR:

<xsl:key name="pair-by-name" match="parent/pair" use="name" />
<xsl:template match="/">
    <parent>
        <xsl:for-each select="parent/pair[count(. | key('pair-by-name', name)[1]) = 1]">
             <xsl:sort select="name" />
            <pair>
            <name>
            <xsl:value-of select="name"/>
            </name>
            <value>
            <xsl:value-of select="sum(key('pair-by-name', name)/value)"/>
            </value>
            </pair>
        </xsl:for-each>
    </parent>
</xsl:template>

The above code should work.

Hikmat
  • 450
  • 4
  • 19