Solved

How to sum values in nodes (XSL)

Posted on 2001-08-08
8
501 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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