Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 17th, 2013, 10:10 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Summation of elements values in a loop

Hi All,

I have a requirement to sum the values in an element where few attribute values(like attribute1, attribute2,attribute 3) are same.
I want to do the same in xslt1.0.

Input payload:

<Lines>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XY</Attribute3>
<Value>10</Value>
</Line>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XY</Attribute3>
<Value>10</Value>
</Line>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>111</Attribute2>
<Attribute3>XYZ</Attribute3>
<Value>15</Value>
</Line>
<Line>
<Attribute1>B</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XYZ</Attribute3>
<Value>10</Value>
</Line>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XY</Attribute3>
<Value>15</Value>
</Line>
<Line>
<Attribute1>B</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XYZ</Attribute3>
<Value>10</Value>
</Line>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XY</Attribute3>
<Value>30</Value>
</Line>
</Lines>

The resultant output should be

<Lines>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XY</Attribute3>
<Value>65</Value>(sum of 10+10+15+30)
</Line>
<Line>
<Attribute1>A</Attribute1>
<Attribute2>111</Attribute2>
<Attribute3>XYZ</Attribute3>
<Value>15</Value>(only 1 node)
</Line>
<Line>
<Attribute1>B</Attribute1>
<Attribute2>11</Attribute2>
<Attribute3>XYZ</Attribute3>
<Value>20</Value>(10+10)
</Line>
<Lines>

If the values in Attribute1, Attribute2 and Attribute 3 are same then the value in "Value" element should add.
Anyone please help me how to resolve this!!!
Thanks in advance!!!


SY
 
Old April 17th, 2013, 10:55 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Use Muenchian grouping, see http://www.jenitennison.com/xslt/gro...muenchian.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="k1" match="Line" use="concat(Attribute1, '|', Attribute2, '|', Attribute3)"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Line[generate-id() = generate-id(key('k1', concat(Attribute1, '|', Attribute2, '|', Attribute3))[1])]">
  <xsl:copy>
    <xsl:copy-of select="*[not(self::Value)]"/>
    <Value><xsl:value-of select="sum(key('k1', concat(Attribute1, '|', Attribute2, '|', Attribute3))/Value)"/></Value>
  </xsl:copy>
</xsl:template>

<xsl:template match="Line[not(generate-id() = generate-id(key('k1', concat(Attribute1, '|', Attribute2, '|', Attribute3))[1]))]"/>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Summation of element vaoues in a loop saralay XSLT 1 April 15th, 2013 05:08 AM
Creating Dynamic Elements with Values from Other Element values chilly XSLT 18 February 24th, 2011 10:31 AM
Insert multiple elements based on values of others fixit XSLT 14 September 25th, 2009 05:36 AM
HTML table with missing values (elements) go4java XSLT 6 July 10th, 2006 03:31 AM
loop values and text box values move mateenmohd Classic ASP Basics 2 April 5th, 2005 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.