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 March 11th, 2010, 09:53 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default Global variables and for-each

Hi there!
I've got xml:

<root>
<profile>
<name>xyz<name>
<salary>4<salary>
</profile>
<profile>
<name>mno<name
<salary>5<salary>
</profile>
</root>

and try to sum all the salary values and place it into table. I tried something like this:

<xsl:for-each select="root/profile">
<xsl:variable name="t"><xsl:value-of select="salary"/></xsl:variable>
<xsl:value-of select="sum($t)"/>;
</xsl:for-each>

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

but I can't get correct value of variable "t" outside the for-each. Please help me, what I've doing wrong?
 
Old March 11th, 2010, 10:12 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well
Code:
<xsl:value-of select="sum(/root/profile/salary)"/>
sums up the salaries. Your attempt with the for-each has a 'profile' element as a context node, then sets the variable 't' to the value of the single 'salary' child element and then takes the sum of a single value.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 11th, 2010, 10:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you sample XML input was actually valid XML you could use something like this:

<td><xsl:value-of select="sum(/root/profile/salary/text())"/></td>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 11th, 2010, 10:54 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Great! It:

<td><xsl:value-of select="sum(/root/profile/salary/text())"/></td>

works fine!
And one more question, how can I get a number of nodes? I'd like to calculate sum/number of nodes, what should I do? In this example (4+5)/2
 
Old March 11th, 2010, 11:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

count()

http://www.w3schools.com/xpath/xpath...s.asp#sequence
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 11th, 2010, 03:29 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Remember that sum() and count() are functions that take a sequence (of numbers or nodes) as the argument. So it doesn't make sense to call them within a for-each that is iterating over that sequence - they need to be called once for the whole sequence, not once for each item within the sequence.
__________________
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
global variables Komila ASP.NET 3.5 Basics 2 October 31st, 2008 01:14 PM
Global Variables in C# su C# 2 December 11th, 2006 06:18 PM
Global variables zarol Beginning PHP 3 May 10th, 2005 06:17 AM
Global variables ps124 ASP.NET 1.0 and 1.1 Basics 6 April 6th, 2004 02:48 PM





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