Re: Aggregate

Subject: Re: Aggregate
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 2 Nov 2000 17:23:04 -0400 (EST)

>I would like to know if we can aggregete a set of xml data. I want the
>average of time in this file which should be (10+12+8)/3. The answer would
>be 10

The average in your example is the sum of the value of the 'time' elements
divided by the number of 'time' elements in the source.  You can get all
the time elements in the source with the XPath:


and put those nodes into a variable with an xsl:variable element:

  <xsl:variable name="times" select="//time" />

Once you've defined that variable, you can get the sum of the values of
those nodes using the sum() function:


and you can count how many there are with the count() function:


Division in XPath is done with the 'div' operator, so to get the average,
you can use:

  sum($times) div count($times)

You can put this in a variable:

  <xsl:variable name="average" select="sum($times) div count($times)" />

and then use it as you will, such as outputting it with xsl:value-of:

  <xsl:value-of select="$average" />

>I am using Schemas and XML so where do i do the average in Schemas or XML
>please respond at the earliest.

The above talks about how to calculate the average within an XSLT
stylesheet that processes some source XML.  I don't understand what you
mean about doing the average in Schemas or XML.  What would doing the
average in Schemas involve?  What would doing it in XML involve?

I hope that this helps anyway,


Jeni Tennison

 XSL-List info and archive:

Current Thread