Solved

How to sum values in nodes (XSL)

Posted on 2001-08-08
8
474 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 200 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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 …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

823 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