fn:avg function

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

Syntax

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

Input items of type xs:untypedAtomic are cast to xs:double.

Returned value

If sequence-expression is not the empty sequence, the returned value is the average 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, the empty sequence is returned.

If sequence-expression has two or more of the following data types, an error is returned:
  • xs:dayTimeDuration
  • xs:yearMonthDuration
  • numeric data types

Example

The following function returns the average of the sequence (5, 1.0E2, 40.5):

fn:avg((5, 1.0E2, 40.5))

The values are promoted to the xs:double data type. The function returns the xs:double value 4.85E1, which is serialized as "48.5".