Solved

Calculate a Total in XML

Posted on 2011-02-18
5
678 Views
Last Modified: 2012-05-11
Trying to calculate a total Item weight. I have very little experience in xml and I thought this is what I would need.

<xsl:for-each select="//Order/Item" > 
    <xsl:variable name="tw" select='$tw + <xsl:value-of select = "/Weight"/>' />
</xsl:for-each>

but it is not valid.
or something like this

<xsl:for-each select="//Order/Item" > 
    <xsl:variable name="tw" select='$tw + /Weight' />
</xsl:for-each>
0
Comment
Question by:frtools
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:suvmitra
ID: 34927803
Why not use your data table (sql, access etc.) to do the calculation and then return the data set and write xml?
0
 
LVL 9

Assisted Solution

by:suvmitra
suvmitra earned 200 total points
ID: 34927831
1. sum(/Students/Student//Marks)
Will sum the marks for all Students, or if you're looping through all Students

2. sum(//Marks)

(assuming that the XPath is operating from within a loop of Students).
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 300 total points
ID: 34931771
First some background on your first attempt
<xsl:for-each select="//Order/Item" >
    <xsl:variable name="tw" select='$tw + <xsl:value-of select = "/Weight"/>' />
</xsl:for-each>

Note that variables in XSLT don't vary (they are more like constants)
Variables in XSLT get a value assigned when they are created
It makes variables useless for calculating sums as you are doing here

You have however full acces to the full XML tree using XPath.
What you need to do for a simple sum is just list the nodes you want to sum in the sum function
You could simply rewrite the above as

<xsl:variable name="tw" select="sum(//Order/Item/Weight)"/>

now $tw contains your sum and you can use it

<xsl:value-of select="$tw"/>

For complex sums (sums from calculated values such as sub-totals)
you can use recursion, which is a comon technique in XSLT to work around constant variables

This might get complex, and at some point you could indeed think about solving the totalling earlier in the chain
(eg. in the database export allready)
But you need to know that a lot of complex totalling in XSLT is possible if you don't have access to the XML creation process

There is a little bug in 2. of suvmitra's second comment
sum(//Marks)
sums all Marks in the document, starting from the root, not from the Students level when you are in context of a student
if you need to be selective in your context (Students), you will need this
sum(.//Marks)
0
 

Author Closing Comment

by:frtools
ID: 34933265
I gave gertone more points because the solution had a better explanation that led me to the right solution.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34933273
welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extract data from xml in c# 8 51
Vb.net XML Read all nodes and 4 73
How do i send an api key in a request header 3 57
Problem to event 3 78
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now