[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Excluding values from XSL Sum.

Posted on 2011-03-02
5
Medium Priority
?
590 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

649 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