Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Arithmetics (5/6) >

Average

Function avg() calculates average of all values of a sequence. All items of the sequence must be transformable to a numeric value. If the argument is an empty sequence, an empty sequence is returned.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <bbb>
                        <xsl:value-of  select="avg(1 to 99)"/>
                  </bbb>
                  <ccc>
                        <xsl:value-of  select="avg(a)"/>
                  </ccc>
                  <ddd>
                        <xsl:value-of  select="avg(b)"/>
                  </ddd>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <a>5</a>
            <a>3</a>
            <a>7</a>
            <a>2</a>
      </aaa>
Output

      <bbb>50</bbb>
      <ccc>4.25</ccc>
      <ddd/>


Previous chapter: Implicit behaviour
Next chapter: Functions operating on strings
Previous page: Summation
Next page: Minima and maxima