Version:

Loading TOC...

fn.sum( $arg as Sequence | Array, [$zero as xs.anyAtomicType?] ) as xs.anyAtomicType?

Returns a value obtained by adding together the values in $arg. If $zero is not specified, then the value returned for an empty sequence is the xs:integer value 0. If $zero is specified, then the value returned for an empty sequence is $zero.

Any values of type xs:untypedAtomic in $arg are cast to xs:double. The items in the resulting sequence may be reordered in an arbitrary order. The resulting sequence is referred to below as the converted sequence.

If the converted sequence is empty, then the single-argument form of the function returns the xs:integer value 0; the two-argument form returns the value of the argument $zero.

If the converted sequence contains the value NaN, NaN is returned.

All items in $arg must be numeric or derived from a single base type. In addition, the type must support addition. Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values. For numeric values, the numeric promotion rules defined in 6.2 Operators on Numeric Values are used to promote all values to a single common type. The sum of a sequence of integers will therefore be an integer, while the sum of a numeric sequence that includes at least one xs:double will be an xs:double.

If the above conditions are not met, a type error is raised [err:FORG0006].

Otherwise, the result of the function, using the second signature, is the result of the expression:

if (fn:count($c) eq 0) then $zero else if (fn:count($c) eq 1) then $c[1] else $c[1] + fn:sum(subsequence($c, 2))

where $c is the converted sequence.

The result of the function, using the first signature, is the result of the expression:fn:sum($arg, 0).

For detailed type semantics, see Section 7.2.10 The fn:min, fn:max, fn:avg, and fn:sum functions[FS].

Notes:

The second argument allows an appropriate value to be defined to represent the sum of an empty sequence. For example, when summing a sequence of durations it would be appropriate to return a zero-length duration of the appropriate type. This argument is necessary because a system that does dynamic typing cannot distinguish "an empty sequence of integers", for example, from "an empty sequence of durations".

If the converted sequence contains exactly one value then that value is returned.

Parameters | |
---|---|

$arg | The sequence of values to be summed. |

$zero | The value to return as zero if the input sequence is the empty sequence. This parameter is not available in the 0.9-ml XQuery dialect. |

`fn:sum`

; the second ($zero) argument is available
in both the 1.0 and 1.0-ml dialects.
const d1 = xs.yearMonthDuration("P20Y"); const d2 = xs.yearMonthDuration("P10M") const seq1 = [d1, d2]; fn.sum(seq1); => an xs:yearMonthDuration with a value of 250 months (P20Y10M). ***** const seq3 = [3, 4, 5]; fn.sum(seq3); => 12 ***** fn.sum(null); => 0 ***** fn.sum(null, null); => ()

Copyright © 2017 MarkLogic Corporation. MARKLOGIC is a
registered trademark of MarkLogic Corporation.

## Comments