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()

the sequence of values to add

zero:numeric()

the value returned when the sequence is empty; optional, 0 by default

Examples

The examples below use this input document:

<xsl:variable name="ordDoc" select="doc('http://www.functx.com/input/order.xml')"/>

<!-- content of order.xml -->
<order num="00299432" date="2006-09-15" cust="0221A">
  <item dept="WMN" num="557" quantity="1" color="beige"/>
  <item dept="ACC" num="563" quantity="1"/>
  <item dept="ACC" num="443" quantity="2"/>
  <item dept="MEN" num="784" quantity="1" color="blue/white"/>
  <item dept="MEN" num="784" quantity="1" color="blue/red"/>
  <item dept="WMN" num="557" quantity="1" color="sage"/>
</order>

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