[2.0] `avg()`

Given a sequence, returns the average value of the items in the sequence.

xs:anyAtomicType?`avg(`

`xs:anyAtomicType*`

`)`

A sequence of values.

The average of the given sequence. You can calculate
averages for six different datatypes: `xs:integer`

, `xs:double`

, `xs:decimal`

, `xs:float`

, `xs:yearMonthDuration`

, and `xs:`

`day`

`Time`

`Duration`

.

Given a sequence of numeric values, the XSLT processor returns the average of those numbers, converting datatypes as necessary. Given a sequence of durations, the XSLT processor returns the average of those durations.

The `avg()`

function
assumes you’ll send it a sequence containing sensible data; if
not, the XSLT processor throws an error. Asking for the average of
the sequence `(42, 57, 'blue')`

returns an error, as you’d expect.

Some notes about how the `avg()`

function works:

To calculate the average of a sequence of durations, all the values must be

`xs:dayTimeDuration`

s or`xs:yearMonthDuration`

s. You can’t mix the two types of durations; if you do, the XSLT processor throws an error.If all the items in the sequence are of type

`xs:untypedAtomic`

, the XSLT processor attempts to cast each value to`xs:double`

. If*any*item in the sequence can’t be converted to an`xs:double`

, the XSLT processor throws an error.Finally, if you pass the

`avg()`

function the empty sequence, the function returns the empty sequence. Although you’re not giving the function any useful data in this case, the XSLT processor doesn’t throw an error.

XQuery 1.0 and XPath 2.0 ...

Start Free Trial

No credit card required