0
votes

For the following XML,

<numbers>
  <number>33</number>
  <number>34.4</number>
  <number>33.8</number>
  <number>33.43</number>
  <number>34.46</number>
  <number>35</number>
  <number>33.49</number>
  <number>33.00</number>
</numbers>

how can I write an XPath expression that will return the sum of all numbers which round off to 34?

Desired selection (text output): 102.66

1
If the answer below solves your problem, please accept it. If not, please follow-up and say how we might help. Thanks. - kjhughes

1 Answers

2
votes

Both of the following XPath expressions will return 102.66 (the sum of the numbers that round to 34) as requested:

XPath 1.0 Solution (Works in XPath 2.0 too)

sum(//number[round(.) = 34])

XPath 2.0 Solution (Just to show a for loop example)

sum(for $n in //number return if (round($n) = 34) then $n else 0)