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 15th, 2013, 12:55 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Summation of element vaoues in a loop

Hi all,

My requirement is to sum the monetary amount values for all the same ReleaseGid/Gid/Xid values.

Ex:
input payload:
<AllocationBase>
<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302179</Xid>
</Gid></ReleaseGid>
<Cost>
<FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>250</MonetaryAmount>
</FinancialAmount></Cost>
</AllocReleaseDetail>

<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302179</Xid>
</Gid></ReleaseGid>
<Cost><FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>350</MonetaryAmount>
</FinancialAmount></Cost>
</AllocReleaseDetail>

<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302180</Xid>
</Gid></ReleaseGid>
<Cost><FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>550</MonetaryAmount>
</FinancialAmount></Cost>
</AllocReleaseDetail>

<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302180</Xid>
</Gid></ReleaseGid>
<Cost><FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>150</MonetaryAmount>
</FinancialAmount></Cost>
</AllocReleaseDetail>

<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302180</Xid>
</Gid></ReleaseGid>
<Cost><FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>300</MonetaryAmount>
</FinancialAmount></Cost>
</AllocReleaseDetail>
</AllocationBase>

Output payload:

<AllocationBase>
<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302179</Xid>
</Gid></ReleaseGid>
<Cost>
<FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>600</MonetaryAmount> <!-- sum of monetary amount for all the elements having ReleaseGid/Gid/Xid =302179 -->
</FinancialAmount></Cost>
</AllocReleaseDetail>

<AllocReleaseDetail>
<ReleaseGid><Gid>
<Xid>302180</Xid>
</Gid></ReleaseGid>
<Cost><FinancialAmount>
<GlobalCurrencyCode>USD</GlobalCurrencyCode>
<MonetaryAmount>1000</MonetaryAmount> <!-- sum of monetary amount for all the elements having ReleaseGid/Gid/Xid =302180 -->
</FinancialAmount></Cost>
</AllocReleaseDetail>
</AllocationBase>


Kindly help me to resolve the issue!!!

Thanks in advance!!!

Sarala
 
Old April 15th, 2013, 05:08 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Code:
<xsl:for-each-group select="//Xid">
  Xid: <xsl:value-of select="current-grouping-key()" Total: <xsl:value-of select="sum(current-group())"/>
</xsl:for-each-group>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify element based on the element data/content ROCXY XSLT 7 September 8th, 2011 02:48 AM
summation of a attribute in multiple similar tags Divya XSLT 5 June 17th, 2010 01:53 AM
Summation of calculated date difference? DavrosSpignot XSLT 6 October 7th, 2007 05:35 PM
Element loop Neal XSLT 1 May 25th, 2006 11:06 AM
Getting form element names in a loop crapanz Javascript 5 January 30th, 2006 12:45 AM





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