sum function

The fn:sum function returns the sum of the values in a sequence.

Syntax

Read syntax diagramSkip visual syntax diagramfn:sum( sequence-expression, empty-sequence-replacement)
sequence-expression
A sequence that contains items of any of the following atomic types, or an empty sequence:
  • xs:float
  • xs:double
  • xs:decimal
  • xs:integer
  • xdt:untypedAtomic
  • xdt:dayTimeDuration
  • xdt:yearMonthDuration
  • A type that is derived from any of the previously listed types

Input items of type xdt:untypedAtomic are cast to xs:double. After this casting, all of the items in the input sequence must be convertible to a common type by promotion or subtype substitution. The sum is computed in this common type. For example, if the input sequence contains items of type money (derived from xs:decimal) and stockprice (derived from xs:float), the sum is computed in the type xs:float.

empty-sequence-replacement
The value that is returned if sequence-expression is the empty sequence. empty-sequence-replacement can have one of the data types that is listed for sequence-expression.

Returned value

If sequence-expression is not the empty sequence, the returned value is the sum of the values in sequence-expression. The data type of the returned value is the same as the data type of the items in sequence-expression, or the data type to which the items in sequence-expression are promoted.

If sequence-expression is the empty sequence, and empty-sequence-replacement is not specified, fn:sum returns 0.0E0. If sequence-expression is an empty sequence, and empty-sequence-replacement is specified, fn:sum returns empty-sequence-replacement.

Example

The following function returns the sum of the sequence (500, 1.0E2, 40.5):

fn:sum((500, 1.0E2, 40.5))

The values are promoted to the xs:double data type. The function returns the xs:double value 6.405E2, which is serialized as "640.5".