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 June 16th, 2010, 06:11 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default summation of a attribute in multiple similar tags

Hi,

Input xml:
<LPS Request>
<summary>
<Date></Date>
<amount>500</amount>
</summary>
<summary>
<Date></Date>
<amount>1500</amount>
</summary>
<summary>
<Date></Date>
<amount>2500</amount>
</summary>
<summary>
<Date></Date>
<amount>3500</amount>
</summary>
</LPS Request>

Output xml should have an attribute input amount with summation of all the amount attribute from multiple summary tags

output xml:

<LPS Request>
<InputAmount>8000</InputAmount> [500+1500+2500+3500]
</LPS Request>

Could any one please help me in this .
 
Old June 16th, 2010, 06:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Firstly - you mention 'attribute' in your subject line. There are no attributes in your XML, only elements.

The Xpath sum() method will do what you want. It will be different depending on the context of where you place it. <xsl:value-of select="sum(Date/amount)"/> for example.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 16th, 2010, 06:33 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi sam,

i wrote xslt like

<LPS Response>
<InputAmount>
<xsl:for-each select="LPS Request/summary">
<xsl:value-of select="sum(amount)"/>
<InputAmount>
</LPS Response>


i got the output as

<LPS Response>
<InputAmount>500150025003500</InputAmount>
</LPS Response>
 
Old June 16th, 2010, 06:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That's strange, because neither your input file, nor your XSLT file are even valid XML, so I don't know how you manage to output anything.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 16th, 2010, 06:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're saying "for each summary, give me the sum of the amounts in that summary", and that's exactly what you are getting. But you want a single sum, not one for each summary. That's sum(summary/amount), executed with LPSRequest as the context item.

If you're going to post XML/XSLT that is abstracted from the real XML/XSLT, please test it before you post it. When people make transcription errors in their posts, it's hard to tell whether that's the real problem or just a distraction caused by careless posting.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 17th, 2010, 01:53 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

I corrected your input xml as below
Code:
<LPS_Request>
<summary>
<Date></Date>
<amount>500</amount>
</summary>
<summary>
<Date></Date>
<amount>1500</amount>
</summary>
<summary>
<Date></Date>
<amount>2500</amount>
</summary>
<summary>
<Date></Date>
<amount>3500</amount>
</summary>
</LPS_Request>
and used the below code
Code:
<xsl:template match="LPS_Request">
<LPS_Response>
<InputAmount>
<xsl:value-of select="sum(summary/amount)"/>
</InputAmount>
</LPS_Response>
</xsl:template>
If this is not what you need, then post an error-free relevant input xml.
__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't find similar html tags in other themes erayman BOOK: WordPress 24-Hour Trainer 1 March 9th, 2010 07:26 PM
How to match multiple xml tags separately? JoshC XSLT 3 February 13th, 2009 07:39 PM
Summation of calculated date difference? DavrosSpignot XSLT 6 October 7th, 2007 05:35 PM
Sorting on similar tags question terrypink XSLT 21 April 2nd, 2007 02:55 PM
UserControls and multiple form tags shenku C# 0 October 14th, 2005 09:08 AM





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