XSL: sum function (adding values in xml)

  • dogbert
  • Novice
  • Novice
  • User avatar
  • Posts: 28
  • Loc: Tacoma, WA

Post 3+ Months Ago

Hello,

I've looked around the net for help and tried a number of ways to get what I want with no working solution to what I'm trying to do, which is bothering me as it seems like it should be very easy. I want to add up elements in my XML doc, simple as that.

example xml file. adding.xml
Code: [ Select ]
 
<?xml version="1.0"?>
<things>
     <thing>
          <name>one</name>
          <description>one</description>
          <numbertoadd>1.2</numbertoadd>
     </thing>
     <thing>
          <name>two</name>
          <description>two</description>
          <numbertoadd>2.3</numbertoadd>
     </thing>
     <thing>
          <name>three</name>
          <description>three</description>
          <numbertoadd>3.2</numbertoadd>
     </thing>
</things>
 
  1.  
  2. <?xml version="1.0"?>
  3. <things>
  4.      <thing>
  5.           <name>one</name>
  6.           <description>one</description>
  7.           <numbertoadd>1.2</numbertoadd>
  8.      </thing>
  9.      <thing>
  10.           <name>two</name>
  11.           <description>two</description>
  12.           <numbertoadd>2.3</numbertoadd>
  13.      </thing>
  14.      <thing>
  15.           <name>three</name>
  16.           <description>three</description>
  17.           <numbertoadd>3.2</numbertoadd>
  18.      </thing>
  19. </things>
  20.  


What I want to do is add the "numbertoadd" elemets from each in this case resulting in 6.7

If it matters, I'm using classic asp to format the xml with an xsl stylesheet to be able to put it where I like.
example adding.asp
Code: [ Select ]
 
<%
 
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("adding.xml"))
 
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("adding.xsl"))
 
    Response.Write(xml.transformNode (xsl))
 
    Response.write("show that it does something.......")
   
%>
 
  1.  
  2. <%
  3.  
  4. set xml = Server.CreateObject("Microsoft.XMLDOM")
  5. xml.async = false
  6. xml.load(Server.MapPath("adding.xml"))
  7.  
  8. set xsl = Server.CreateObject("Microsoft.XMLDOM")
  9. xsl.async = false
  10. xsl.load(Server.MapPath("adding.xsl"))
  11.  
  12.     Response.Write(xml.transformNode (xsl))
  13.  
  14.     Response.write("show that it does something.......")
  15.    
  16. %>
  17.  


Any and all help or guidence is very much appreciated.

Thanks,
Chris (dogbert)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Would this help you in any way?


Another post:
Quote:
Just select the nodes that you want to sum with an Xpath and then apply
sum() to those.


Don't know if those would help... just something I found searching google.
  • dogbert
  • Novice
  • Novice
  • User avatar
  • Posts: 28
  • Loc: Tacoma, WA

Post 3+ Months Ago

Pointed me to the right direction but after messing with it and other examples I've found I have not had luck, relly bugs me, so I decided to move to a solution that I have more knoledge with.

my shot at just pulling the data and doing the math within the asp file.

adding.xsl
Code: [ Select ]
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
      <xsl:for-each select="dailytasks/task">
 
        <xsl:value-of select="tasktime" />#
      </xsl:for-each>
 
</xsl:template>
 
</xsl:stylesheet>
 
  1.  
  2. <?xml version="1.0" encoding="ISO-8859-1"?>
  3. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  4. <xsl:template match="/">
  5.       <xsl:for-each select="dailytasks/task">
  6.  
  7.         <xsl:value-of select="tasktime" />#
  8.       </xsl:for-each>
  9.  
  10. </xsl:template>
  11.  
  12. </xsl:stylesheet>
  13.  


adding.asp
Code: [ Select ]
 
<%
 
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("adding.xml"))
 
On Error Resume Next
 
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("adding.xsl"))
 
On Error Resume Next
 
Dim numbr, num, valu, length, fnl, kickr, kickrn
 
    numbr = xml.transformNode(xsl)
       
    num = split(numbr, "#")
   
    kickr = Cdbl(num(0))
   
    length = Ubound(num)
   
    valu = 0
   
    for x = 0 to Ubound(num)
        valu = (valu + Cdbl(num(x)))
    next
   
        fnl = (valu + kickr)
   
On Error Resume Next
   
   
%>
<%= numbr %> <em>: all together no format<br/></em>
<br/>Static Counts:<br/><br/>
<%= num(0) %> <em>: First After The Split<br/></em>
<%= num(1) %> <em>: Seccond After The Split<br/></em>
<%= num(2) %> <em>: Third After The Split<br/></em>
<%= num(3) %> <em>: Fourth After The Split<br/></em>
<%= num(4) %> <em>: Fifth After The Split<br/></em>
<%= num(5) %> <em>: Sixth After The Split<br/></em>
<%= num(6) %> <em>: Seventh After The Split<br/></em>
<%= num(7) %> <em>: Eight After The Split<br/><br/></em>
<%= length %> <em>: Lenght of the Array<br/><br/></em>
<%= valu %> <em>: Final Before the Kicker<br/></em>
<%= kickr %> <em>: The Kicker Value<br/><br/></em>
<%= fnl %> <em>: The Total Hours Spend per the adding.xml log - Should be "8"<br/></em>
 
  1.  
  2. <%
  3.  
  4. set xml = Server.CreateObject("Microsoft.XMLDOM")
  5. xml.async = false
  6. xml.load(Server.MapPath("adding.xml"))
  7.  
  8. On Error Resume Next
  9.  
  10. set xsl = Server.CreateObject("Microsoft.XMLDOM")
  11. xsl.async = false
  12. xsl.load(Server.MapPath("adding.xsl"))
  13.  
  14. On Error Resume Next
  15.  
  16. Dim numbr, num, valu, length, fnl, kickr, kickrn
  17.  
  18.     numbr = xml.transformNode(xsl)
  19.        
  20.     num = split(numbr, "#")
  21.    
  22.     kickr = Cdbl(num(0))
  23.    
  24.     length = Ubound(num)
  25.    
  26.     valu = 0
  27.    
  28.     for x = 0 to Ubound(num)
  29.         valu = (valu + Cdbl(num(x)))
  30.     next
  31.    
  32.         fnl = (valu + kickr)
  33.    
  34. On Error Resume Next
  35.    
  36.    
  37. %>
  38. <%= numbr %> <em>: all together no format<br/></em>
  39. <br/>Static Counts:<br/><br/>
  40. <%= num(0) %> <em>: First After The Split<br/></em>
  41. <%= num(1) %> <em>: Seccond After The Split<br/></em>
  42. <%= num(2) %> <em>: Third After The Split<br/></em>
  43. <%= num(3) %> <em>: Fourth After The Split<br/></em>
  44. <%= num(4) %> <em>: Fifth After The Split<br/></em>
  45. <%= num(5) %> <em>: Sixth After The Split<br/></em>
  46. <%= num(6) %> <em>: Seventh After The Split<br/></em>
  47. <%= num(7) %> <em>: Eight After The Split<br/><br/></em>
  48. <%= length %> <em>: Lenght of the Array<br/><br/></em>
  49. <%= valu %> <em>: Final Before the Kicker<br/></em>
  50. <%= kickr %> <em>: The Kicker Value<br/><br/></em>
  51. <%= fnl %> <em>: The Total Hours Spend per the adding.xml log - Should be "8"<br/></em>
  52.  


result being very close however it will not let me convert the first value in the num array "num(0)" to an decimal integer to be added thus It does not add it. note the array skips num(0) because the loop has to start at 1 hence I'm adding it in afterward. It works perfect except for the first number, tried with other xml files with the same result.
Code: [ Select ]
 
0.50# 0.25# 0.50# 0.50# 1.50# 0.50# 0.50# 3.75# : all together no format
 
Static Counts to check:
 
0.50 : First After The Split
0.25 : Seccond After The Split
0.50 : Third After The Split
0.50 : Fourth After The Split
1.50 : Fifth After The Split
0.50 : Sixth After The Split
0.50 : Seventh After The Split
3.75 : Eight After The Split
 
8 : Lenght of the Array
 
7.5 : Final Before the Kicker
: The Kicker Value
 
7.5 : The Total Number per the adding.xml log - Should be "8"
 
  1.  
  2. 0.50# 0.25# 0.50# 0.50# 1.50# 0.50# 0.50# 3.75# : all together no format
  3.  
  4. Static Counts to check:
  5.  
  6. 0.50 : First After The Split
  7. 0.25 : Seccond After The Split
  8. 0.50 : Third After The Split
  9. 0.50 : Fourth After The Split
  10. 1.50 : Fifth After The Split
  11. 0.50 : Sixth After The Split
  12. 0.50 : Seventh After The Split
  13. 3.75 : Eight After The Split
  14.  
  15. 8 : Lenght of the Array
  16.  
  17. 7.5 : Final Before the Kicker
  18. : The Kicker Value
  19.  
  20. 7.5 : The Total Number per the adding.xml log - Should be "8"
  21.  
  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

You can sum those nodes in quite a simple manner, like so:

Code: [ Select ]
<?xml version="1.0" encoding="UTF-8"?>
 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
 
<xsl:template match="/">
 
<total><xsl:value-of select='sum(/things/thing/numbertoadd)'/></total>
 
</xsl:template>
</xsl:stylesheet>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <xsl:stylesheet version="1.0"
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5. <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
  6.  
  7. <xsl:template match="/">
  8.  
  9. <total><xsl:value-of select='sum(/things/thing/numbertoadd)'/></total>
  10.  
  11. </xsl:template>
  12. </xsl:stylesheet>


The xslt I've written may be to be simplistic for your needs, but it does sum the values of the "numbertoadd" nodes to 6.7
  • dogbert
  • Novice
  • Novice
  • User avatar
  • Posts: 28
  • Loc: Tacoma, WA

Post 3+ Months Ago

See that was exactly what I was looking for, I dont konw why I could not get the sum function to work at all. But that seems to work for me, could it have been the indent='yes'?

Anyway, Much thanks.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 68 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.