[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: does xslt support max(), min() and avg() functions

Subject: Re: does xslt support max(), min() and avg() functions ??
From: "Steve Muench" <Steve.Muench@xxxxxxxxxx>
Date: Wed, 14 Feb 2001 10:25:26 -0800
xslt avg
| > i want to get the maximum, minimum and average values of xml elements....i
| > want to use XSLTs for this purpose...does XSLT support mathematical functions
| > ornot ??
| > how can i implement mathematical functions using XSLTs ??
| From: "Michael Hoffmann" <m-hoffmann@xxxxxx>
| 
| could you PLEASE buy a book and read it ??
| or just search for XML articles online.. nearly all of your questions (of your
| older posts too) will be answered there

That sounds kind of harsh... :-)

Let's try to help out if we can.

XSLT supports count() and sum() aggegate functions.

By using a trick to assign a variable to a computed value,
you can use XSLT to calculate min, max, and avg like this:

Let's say your source document is:

  <list>
    <item>15</item>
    <item>10</item>
    <item>20</item>
  </list>

Then it's easy to assign a value to the sum() of the items
by doing:
   
   <xsl:variable name="the_sum" value="sum(/list/item)"/>

or the count of the items:

   <xsl:variable name="the_count" value="count(/list/item)"/>

for the average, you can do something like this:

   <xsl:variable name="the_avg" 
     select="sum(/list/item) div count(/list/item)"/>

for the max, you can do:

   <!--
    | assign variable based on picked the first item in
    | the numerically-sorted-descending list of items.
    +>
   <xsl:variable name="the_max">
     <xsl:for-each select="/list/item">
       <xsl:sort data-type="number" order="descending"/>
       <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
     </xsl:for-each>
   </xsl:variable>

for the min, you just reverse the sort order:

   <!--
    | assign variable based on picked the first item in
    | the numerically-sorted-descending list of items.
    +>
   <xsl:variable name="the_min">
     <xsl:for-each select="/list/item">
       <xsl:sort data-type="number" order="ascending"/>
       <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
     </xsl:for-each>
   </xsl:variable>


______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly
http://www.oreilly.com/catalog/orxmlapp/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.