xf:sum

Determines the sum of all the items in a sequence.

Signatures

xf:sum(item* $item-var) —> xs:double?

Arguments

Data Type
Argument
Description

item*

$item-var

Represents the sequence to evaluate.

Returns

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.

Examples

Simple

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> 

Empty Sequences

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.

Nodes

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.

Related Topics

W3C sum function description.