Determines the average of all the numbers in a sequence.
If the value of $item-var is the empty sequence, the empty sequence is returned. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.
xf:avg(item* $item-var) —> xs:double?
Returns the average of all the numbers in a sequence passed into $item-var.
Invoking xf:avg(("3","1","2")) returns the value of 2.0, as shown in the following example query:
<result>{xf:avg(("3","1","2"))}</result>
The preceding query generates the following result:
<result>2.0</result>
Invoking xf:avg((1,(),6,2,9)) returns the value of 4.5, as shown in the following example query:
<result>{xf:avg((1,(),6,2,9))}</result>
The preceding query generates the following result:
<result>4.5</result>
Note: Instances of the empty sequence () are ignored.
Invoking the following query returns the value of 50, as shown in the following example query:
let $x := <a>100</a>
let $y := <b>50</b>
let $z := <c>0</c>
return <result>{xf:avg(($x,$y,$z))}</result>
The preceding query generates the following result:
<result>50.0</result>
In this example, the value of the nodes are extracted before the numbers are averaged together—as if the data function had been invoked on each node in the sequence before being averaged together.
W3C avg function description.