avg
The average of a sequence of values
Description
The fn:avg function averages a sequence of values. 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).
Special care should be taken with any "missing" values when using the fn:avg function. See functx:avg-empty-is-zero for more information.
Parameters |
Description |
|---|---|
arg:numeric() |
the sequence of values to average |
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 |
|---|---|
avg( (1, 2, 3, 4, 5) ) |
3 |
avg( (1, 2, 3, (), 4, 5) ) |
3 |
avg((xs:yearMonthDuration('P4M'), xs:yearMonthDuration('P6M') ) ) |
P5M |
1.1666666666666667 |
|
avg( () ) |
() |
avg($ordDoc//item/@dept) |
Error FORG0001 |