Solved

Excluding values from XSL Sum.

Posted on 2011-03-02
5
585 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

729 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