xf:avg

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.

Signatures

xf:avg(item* $item-var) —> xs:double?

Arguments

Data Type
Argument
Description

item*

$item-var

Represents the sequence of numbers to add together.

Returns

Returns the average of all the numbers in a sequence passed into $item-var.

Examples

Simple

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> 

Empty Sequences

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.

Nodes

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.

Related Topics

W3C avg function description.