x
Solved

Calculate a Total in XML

Posted on 2011-02-18
Medium Priority
707 Views
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
Question by:frtools
• 2
• 2

LVL 9

Expert Comment

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

suvmitra earned 800 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

Geert Bormans earned 1200 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

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

ID: 34933273
welcome
0

Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll