 |
| 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
|
|
|
|

January 31st, 2007, 01:21 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
adding up values in xslt
Hi Guys,
Im trying to add up all the values (see example)
<xml>
<site>
<value></value>
<value></value>
.
.
.
.
<site>
<xml>
The problem was in a given xml. The value node could have many instance or could change the number of node.
And in my xslt
<xsl:for-each select="site/value">
{... what's next to add up instance of value node}
</xsl:for-each>;)
Writing Software Application is just setting instructions to the code and there is nothing impossible.
|
|

January 31st, 2007, 01:26 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Just use
<xsl:value-of select="sum(value)"/>
with site as the context node
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

January 31st, 2007, 06:27 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thnx Dude,
MORE POWER to this Forum!!!
Writing Software Application is just setting instructions to the code and there is nothing impossible.
|
|

January 31st, 2007, 07:30 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
Just wondering why it doesnt add up all the values.
sample xml:
<xml>
<site>
<value>1</value>
<value>2</value>
.
.
.
.
<site>
<xml>
sample xsl:
<xsl:for-each select="site/value">
<xsl:value-of select="sum(.)"/>
</xsl:for-each>
Output:
12......
Writing Software Application is just setting instructions to the code and there is nothing impossible.
|
|

February 1st, 2007, 05:43 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
sum() is a function that takes as input a set of nodes containing numeric values, and it produces as output the sum of these values. "." is a node-set containing a single node, and the sum of a set of numbers in which there is only one number is that number. So sum(.) means the same as number(.).
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 1st, 2007, 12:11 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thnx, I appreciate your help.
:D
Writing Software Application is just setting instructions to the code and there is nothing impossible.
|
|

March 21st, 2007, 10:41 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
I need to add all the values like this
<root>
<profile>
<name>xyz<name>
<salary>40000<salary>
<addr>abc</addr>
</profile>
<profile>
<name>mno<name
<salary>50000<salary>
<addr>def</addr>
</profile>
</root>
I tried to get Total salary as 90000 but i am getting 4000050000
using
<xsl:for-each select="root/profile/salary">
<xsl:value-of select="sum(.)"/>
</xsl:for-each>
|
|

March 21st, 2007, 11:40 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
The function sum() expects a node-set as its argument. You are supplying a single node (.), so sum() returns the numeric value of that node. You are then doing a string-concatenation of the results of several calls on the sum function.
You want <xsl:value-of select="sum(root/profile/salary)"/>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

March 21st, 2007, 11:58 AM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi mhkay
Thanks alot, i am looking for a solution from hours. quick response with correct solution :)
|
|

March 25th, 2007, 01:53 PM
|
|
Registered User
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
I need the continuation for the above solution. now i got the sum of all the salaries. now i can able to display in jsp. but i need to store that value in one jsp varible.
i have one confirmation button. when the user clicks on that i have to display the total salary along with the confirmation message. how i can pass that value to javascipt method.
|
|
 |