Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to sum values in nodes (XSL)

Posted on 2001-08-08
8
Medium Priority
?
517 Views
Last Modified: 2008-03-17
This is really starting to annoy me. I've gone through many examples, and it still won't work.. Here is a sample of my XML:

<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="clanx_members.xsl"?>

<root>
  <toon name="Ainwen">
    <level>43</level>
     <race>Wood Elf</race>
     <class>Druid</class>
     <status>Primary</status>
     <contact>Mail Ainwen</contact>
     
     <alt_toon name="Kolien">
       <level>19</level>
       <race>Wood Elf</race>
       <class>Ranger</class>
       <status>Alternate</status>
       <contact>Mail Kolien</contact>
     </alt_toon>    
  </toon>
</root>

What I want to do is sum all <level> nodes. Here is a sample of the xsl that i'm using:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">
    <HTML>
      <BODY>
         <xsl:value-of select="sum(level)"/>
       </BODY>
     </HTML>
  </xsl:template>
</xsl:stylesheet>

I've tried just about every incarnation of sum(level) as I could think of and still i get the following error when i try to load up my xml page:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

NodeTest expected here. -->sum(<--level)
 


If anyone can tell me what I'm doing wrong i'd soooo appreciate it!!

Mike
0
Comment
Question by:Dhag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 6365461
It is very simple. The sum() function takes as its parameter a node-list.

You have specified the name "level" which is to identify this list. The context of the call however is "underneath the root element" since the xsl:template statement has match="/". There is no level element directly underneath the root.

An XPath expression "//level" would search your XML and extract ALL (yes, every single one) level elements and feed them into sum(). If that is what you want to do - voila!
0
 
LVL 27

Expert Comment

by:BigRat
ID: 6365472
Here is the extract from the Microsoft documentation for your futuree reference :-

sum Function
Returns the sum of all nodes in the node set. Each node is first converted to a number value before summing.

number sum(node-set)
Remarks
Given the following XML document:

<root>
  <a>1</a>
  <a>3</a>
  <a>2</a>
</root>
The following function call returns 6.

sum(//a)

See Also XML Data Types Reference
0
 

Author Comment

by:Dhag
ID: 6365480
Unfortunately I tried //level as well. Knowing this you may understand more why i'm so frustrated.

Any other ideas?

Mike
0
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
LVL 27

Expert Comment

by:BigRat
ID: 6365611
Nope, and I'm at home now. But tomorrow morning I'll try it at work. I do have a stylesheet at work which does something like this. Which parser/transformer do you use? I used MSXML3 in IE 5.5.
0
 

Author Comment

by:Dhag
ID: 6365639
I have both MSXML2 and 3 on my machine, and I am using IE 5.5 with pretty much all current updates.

Thanks a million for your efforts :)

Mike
0
 
LVL 4

Accepted Solution

by:
edmund_mitchell earned 800 total points
ID: 6365743
Hello Dhag

When you use:
<xsl:value-of select="sum(//level)"/>
it doesn't work?
I just ran it, and it works fine.
If it's not working for you, I'm betting the problem is that you don't have MSXML3 installed in replace mode, it's in side-by-side mode, and so it's not forcing the transform for you.
Go to:
http://www.netcrucible.com/xslt/msxml-faq.htm
and follow the instructions to be sure that you've got MSXML3 in replace mode, and then go again, and I think it'll work just fine.

Edmund
0
 
LVL 27

Expert Comment

by:BigRat
ID: 6369381
Edmund is probably right. I have just copied and tested YOUR XML and XSL and it works fine!
0
 

Author Comment

by:Dhag
ID: 6374098
That was exactly the problem. I thank you profusely my friend!

This is one of those things that really annoys me with Microsoft though. I mean, BOTH dlls were registered, so why wouldn't the function (or something) know to use the correct dll?

Oh well.. no harm done, except some lost time and a bit of frustration.

Again, thank you muchly, and enjoy the 200 points :)

Mike
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question