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 8th, 2005, 05:31 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calculating sum of attributes

Hi!

I have a problem calculating the sum of attributevalues for bankaccounts (similar to one posted here before I guess.....). The xml-file used is very large,so I have simplified the problem.
What I want to do, is to find the accumulated money to be transferred to each account based on the transferid-attributs for each account.

<account nr=1> should get 234 + 764 + 912 + 1002 = 2912 euros
(transferid="1" + transferid="2" + transferid="5" + transferid="7")
transferred to his account after all the tranfers have been done.

If it is possible, I also want to calculate the accumulated amount money transfered after each transfer:

transferid="1" = 234
transferid="1" + transferid="2" = 998
transferid="1" + transferid="2" + transferid="5" = 1910
transferid="1" + transferid="2" + transferid="5" + transferid="7" = 2912

simplified xxml-file:

<bank>
<account nr="1" transferid="1">
<account nr="1" transferid="2">
<account nr="1" transferid="5">
<account nr="1" transferid="7">
<account nr="2" transferid="2">
<account nr="2" transferid="4">
<account nr="2" transferid="5">
<account nr="2" transferid="6">

<transfer transferid=1 value="234">
<transfer transferid=2 value="764">
<transfer transferid=3 value="912">
<transfer transferid=4 value="1002">
<transfer transferid=5 value="34">
<transfer transferid=6 value="23">
<transfer transferid=7 value="907">
</bank>


I have tried the tip given on the other posting here by using sum(preceding-sibling::......), but it will not work.

Do anyone out there have any solution??


 
Old June 9th, 2005, 08:37 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0:

<xsl:for-each-group select="account" group-by="nr">
  <ans account="{current-grouping-key()"
       total="{sum(/*/transfer[@transferid=current-group()/@transferid]/@value}"/>
</xsl:for-each-group>

XSLT 1.0 is considerably harder, you'll need to look up how to use Muenchian grouping.

In both cases you'll get a speed-up by defining a key to speed the lookup of transfer elements.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 12th, 2005, 04:17 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much. XSLT 2.0 makes the world much more easy!!! This formula helps me with the calculation of the total amount transferred for each account after ALL the transactions have been performed.

The final problem I have, is to calculate the partial sums and also display them.
For account nr=1, I want to display the following:

transferid="1" = 234
transferid="1" + transferid="2" = 998
transferid="1" + transferid="2" + transferid="5" = 1910
transferid="1" + transferid="2" + transferid="5" + transferid="7" = 2912


I have tried to use preceding-sibling, but it will not work:

<xsl:value-of select="current-grouping-key()"/>
<xsl:value-of select="sum(/*/preceding-sibling::transfer[@transferid=current-group()/@transferid]/@value)"/>

Hope someone out there can help me!!

 
Old June 12th, 2005, 05:11 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your error is the "/*" at the start of the path

sum(/*/preceding-sibling::transfer

you don't want to select from "/", the root of the document, you want to select from the context node.

Something like

for-each select="current-group()"
  value-of select="sum(preceding-sibling::transfer[...]/@value)


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating ashik112 ASP.NET 1.0 and 1.1 Basics 2 February 14th, 2007 12:49 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
calculating values aceconcepts PHP How-To 0 February 16th, 2006 09:01 AM
Calculating "Sum" of Numeric values in Varchar fld itHighway Classic ASP Basics 6 December 29th, 2004 07:41 PM





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