Solved

Excluding values from XSL Sum.

Posted on 2011-03-02
5
583 Views
Last Modified: 2012-05-11
What I am doing: I am attempting to sum values.

The problem: The XML file includes characters that cannot be summed. I am unable to edit the XML data file. I need to locate a method to exclude values that match a certain character and sum the remaining values. (eg. 22+ 9 + '-' + 5) Obviously the '-' causes the XSL sum function to return "NaN"

I am still relatively new to XSL and will freely admit that I conceptually know how do to this, and in an OOP way I know what to do, but I do not know the syntax or language of XSL well enough to achieve the required functionality.

While this is not a "for-each" there is only one key (<key>0..10</key> is a sibling of NodeG which is Value) and value pair per occurrence of NodeB.

sum(NodeA/NodeB/NodeC/NodeD/NodeE/NodeF[key='0..10']/NodeG)

The above summation correctly sums values if there are no random non numeric characters. The below XML example contains two "Values"; 29 and -. Currently when running the above sum I get "NaN". I would like to exclude the '-' and get 29.

In attempting to do some research it appears that I might be able to use the "translate(str, str, str) function, but am not sure how this can be incorporated into the sum function.

Thoughts?

Example XML structure:
<NodeA>
  <NodeB>
    <NodeC>
      <NodeD>
        <NodeE>
          <NodeF>
              <key>0..10</key>
              <NodeG>29</NodeG>
           </NodeF>
         </NodeE>
        </NodeD>
      </NodeC>
     </NodeB>
     <NodeB>
    <NodeC>
      <NodeD>
        <NodeE>
          <NodeF>
              <key>0..10</key>
              <NodeG>-</NodeG>
           </NodeF>
         </NodeE>
        </NodeD>
      </NodeC>
     </NodeB>
  </NodeA>
0
Comment
Question by:nmluthy
[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
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 35018380
add a test to see if the node can be casted to a number

sum(NodeA/NodeB/NodeC/NodeD/NodeE/NodeF[key='0..10']/NodeG[not(string(number(.)) = 'NaN')])
0
 

Author Comment

by:nmluthy
ID: 35018473
Thanks Gertone.

If I may ask a follow up questions pertaining to understanding why that works. (Great to have a working solution, but I also want to understand it).

Rephrasing: We get the value (NodeG) an we are only using numbers that do not return a unacceptable (NaN) format? I am a little fuzzy on what the "(.)" means following number.

Thanks again!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35018494
. is current context
number(.) casts the content of the current context (element NodeG) to a number, it will return 'NaN' if not possible
then we cast to string and compare with 'NaN'
if that is not() true we use the node
0
 

Author Comment

by:nmluthy
ID: 35018613
Awesome thanks. I appreciate the knowledge (and assistance on solution).
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35018833
welcome
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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. 
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to count occurrences of each item in an array.

730 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