sum

The sum of values in a sequence

Description

The fn:sum function sums the values in a sequence. The $arg sequence can contain a mixture of numeric and untyped values. Numeric values are promoted as necessary to make them all the same type. Untyped values are cast as numeric xs:double values.

The function can also be used on duration values, so the $arg sequence can contain all xs:yearMonthDuration values or all xs:dayTimeDuration values (but not a mixture of the two).

The $zero argument allows you to specify an alternate value for the sum of the empty sequence. If $arg is the empty sequence, and $zero is provided, the function returns $zero. The $zero argument could be the empty sequence, the integer 0, the value NaN, a duration of zero seconds, or any other atomic value.

Parameters

Description

arg:numeric()

zero:numeric()

Examples

XPath

Results

sum( (1, 2, 3) )

6

sum($ordDoc//item/@quantity)

7

sum($ordDoc//item/@dept)

Error FORG0001

sum( (xs:yearMonthDuration(‘P1Y2M’), xs:yearMonthDuration(‘P2Y3M’)) )

P3Y5M

sum( (1, 2, 3, () ) )

6

sum( (1, 2, xs:yearMonthDuration(‘P1Y’)) )

Error FORG0006

sum( () )

0

sum( (), () )

()

See Also