This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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


| > 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@ti.com>
| 
| 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]