[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

Excluding values from XSL Sum.

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
nmluthy
Asked:
nmluthy
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
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
 
nmluthyAuthor Commented:
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
 
Geert BormansCommented:
. 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
 
nmluthyAuthor Commented:
Awesome thanks. I appreciate the knowledge (and assistance on solution).
0
 
Geert BormansCommented:
welcome
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now