Determines the sum of all the items in a sequence.
xf:sum(item* $item-var) —> xs:double?
Returns the sum of all the numbers in a sequence passed into $item-var.
Returns 0.0 if $item-var is the empty sequence. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.
Invoking xf:sum(("3","1","2")) returns the value of 6, as shown in the following example query:
<result>{xf:sum(("3","1","2"))}</result>
The preceding query generates the following result:
<result>6.0</result>
Invoking xf:sum((1,(),6,2,9)) returns the value of 18, as shown in the following example query:
<result>{xf:sum((1,(),6,2,9))}</result>
The preceding query generates the following result:
<result>18.0</result>
Note: Instances of the empty sequence () are ignored.
Invoking the following query returns the value of 152, as shown in the following example query:
let $x := <a>100</a>
let $y := <b>2</b>
let $z := <c>50</c>
return <result>{xf:sum(($x,$y,$z))}</result>
The preceding query generates the following result:
<result>152.0</result>
In this example, the value of the nodes are extracted before the numbers are added to together—as if the data function had been invoked on each node in the sequence before being added together.
W3C sum function description.