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 January 31st, 2007, 01:21 PM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to northwind Send a message via Yahoo to northwind
Default 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.
 
Old January 31st, 2007, 01:26 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old January 31st, 2007, 06:27 PM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to northwind Send a message via Yahoo to northwind
Default

Thnx Dude,

MORE POWER to this Forum!!!

Writing Software Application is just setting instructions to the code and there is nothing impossible.
 
Old January 31st, 2007, 07:30 PM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to northwind Send a message via Yahoo to northwind
Default

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.
 
Old February 1st, 2007, 05:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old February 1st, 2007, 12:11 PM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to northwind Send a message via Yahoo to northwind
Default

Thnx, I appreciate your help.

:D

Writing Software Application is just setting instructions to the code and there is nothing impossible.
 
Old March 21st, 2007, 10:41 AM
Registered User
 
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>

 
Old March 21st, 2007, 11:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old March 21st, 2007, 11:58 AM
Registered User
 
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay

   Thanks alot, i am looking for a solution from hours. quick response with correct solution :)



 
Old March 25th, 2007, 01:53 PM
Registered User
 
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.








Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt adding text blitzer XSLT 3 May 19th, 2007 10:10 PM
xslt adding image palli2004 XSLT 2 December 30th, 2006 06:35 PM
adding elements in XSLT 1.0 spencer.clark XSLT 3 July 25th, 2005 09:41 AM
Adding Values NeilS21 Beginning VB 6 1 February 6th, 2004 10:45 AM





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