Solved

How to sum values in nodes (XSL)

Posted on 2001-08-08
8
451 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

18 Experts available now in Live!

Get 1:1 Help Now