Here is a small example where the trick in @DevNull's answer doesn't help:
<a> <b>5</b> <c> <d></d> <e>1</e> <f>2</f> </c> </a>
We want : /a/c/d + /a/c/f
To ensure that we receive the amount, although some of them may be empty or not contain numbers, use :
sum((/a/c/d|/a/c/f)[number(.) = number(.)])
Explanation
XPath expression: (/a/c/d|/a/c/f)[number(.) = number(.)]
selects only those from all merged nodes whose value is a number. Therefore, the sum() function will be provided only with numeric arguments and will not create NaN .
The expression number(.) = number(.) Is true only when . is a number.
Here we use that number(somethingNon-Number) is NaN
and that NaN not equal to anything, even before NaN .
source share