Solved

How to sum values in nodes (XSL)

Posted on 2001-08-08
8
461 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Xsl Nested Loop 4 26
router and equipment 3 79
Problem to DocumentFormat 8 74
Which free XML software can I use for opening and editing an XML file? 15 104
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now