XSL: sum function (adding values in xml)
- dogbert
- Novice


- Joined: Dec 06, 2007
- Posts: 28
- Loc: Tacoma, WA
- Status: Offline
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
<?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>
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
<%
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.......")
%>
Any and all help or guidence is very much appreciated.
Thanks,
Chris (dogbert)
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>
- <?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>
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.......")
%>
- <%
- 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.......")
- %>
Any and all help or guidence is very much appreciated.
Thanks,
Chris (dogbert)
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
May 5th, 2009, 3:10 pm
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
- dogbert
- Novice


- Joined: Dec 06, 2007
- Posts: 28
- Loc: Tacoma, WA
- Status: Offline
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
<?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>
adding.asp
<%
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>
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.
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"
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>
- <?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>
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>
- <%
- 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>
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"
- 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"
- Zwirko
- Guru


- Joined: May 29, 2005
- Posts: 1417
- Loc: 55° N, 3° W
- Status: Offline
You can sum those nodes in quite a simple manner, like so:
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
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>
<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>
- <?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>
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
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 5 posts
- Users browsing this forum: No registered users and 79 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
