Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 31st, 2007, 12:21 PM
Registered User
 
Join Date: Jan 2007
Location: Denver, Colorado, USA.
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.
Reply With Quote
  #2 (permalink)  
Old January 31st, 2007, 12:26 PM
mhkay's Avatar
Wrox Author
Points: 18,225, Level: 58
Points: 18,225, Level: 58 Points: 18,225, Level: 58 Points: 18,225, Level: 58
Activity: 66%
Activity: 66% Activity: 66% Activity: 66%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,924
Thanks: 0
Thanked 281 Times in 276 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
Reply With Quote
  #3 (permalink)  
Old January 31st, 2007, 05:27 PM
Registered User
 
Join Date: Jan 2007
Location: Denver, Colorado, USA.
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.
Reply With Quote
  #4 (permalink)  
Old January 31st, 2007, 06:30 PM
Registered User
 
Join Date: Jan 2007
Location: Denver, Colorado, USA.
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.
Reply With Quote
  #5 (permalink)  
Old February 1st, 2007, 04:43 AM
mhkay's Avatar
Wrox Author
Points: 18,225, Level: 58
Points: 18,225, Level: 58 Points: 18,225, Level: 58 Points: 18,225, Level: 58
Activity: 66%
Activity: 66% Activity: 66% Activity: 66%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,924
Thanks: 0
Thanked 281 Times in 276 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
Reply With Quote
  #6 (permalink)  
Old February 1st, 2007, 11:11 AM
Registered User
 
Join Date: Jan 2007
Location: Denver, Colorado, USA.
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.
Reply With Quote
  #7 (permalink)  
Old March 21st, 2007, 10:41 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
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>

Reply With Quote
  #8 (permalink)  
Old March 21st, 2007, 11:40 AM
mhkay's Avatar
Wrox Author
Points: 18,225, Level: 58
Points: 18,225, Level: 58 Points: 18,225, Level: 58 Points: 18,225, Level: 58
Activity: 66%
Activity: 66% Activity: 66% Activity: 66%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,924
Thanks: 0
Thanked 281 Times in 276 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
Reply With Quote
  #9 (permalink)  
Old March 21st, 2007, 11:58 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
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 :)



Reply With Quote
  #10 (permalink)  
Old March 25th, 2007, 01:53 PM
Registered User
 
Join Date: Oct 2006
Location: , , .
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.



Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 05: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 09:45 AM



All times are GMT -4. The time now is 04:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.