Solved

Excluding values from XSL Sum.

Posted on 2011-03-02
5
578 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
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
. 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
Comment Utility
Awesome thanks. I appreciate the knowledge (and assistance on solution).
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 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

10 Experts available now in Live!

Get 1:1 Help Now