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

Maximum (Was Re: counting/comparing values)

Subject: Maximum (Was Re: counting/comparing values)
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 9 Nov 2001 21:35:16 -0800 (PST)
xsl maximum
> I am stuck trying to get the highest number of child elements of a
> particular type.  What I would like to do is return the count of the most
> <entry> elements within a single <row> of a <table>:

In January there was a thread in this group on the solution of exactly the same
problem.

Another, functional solution is the following:

maximum  = foldl1 max

where foldl1 is a foldl function that is defined on non-empty lists

Here's a translation of this into XSLT:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:maximum-pick-bigger="f:maximum-pick-bigger"
xmlns:maximum-own-compare="f:maximum-own-compare"
exclude-result-prefixes="xsl maximum-own-compare maximum-pick-bigger"
>
   <xsl:import href="foldl.xsl"/>

   <maximum-pick-bigger:maximum-pick-bigger/>
   <maximum-own-compare:maximum-own-compare/>

    <xsl:template name="maximum">
      <xsl:param name="pList" select="/.."/>
      <xsl:param name="pCMPFun" select="/.."/>

      <xsl:variable name="vdfCMPFun" 
           select="document('')/*/maximum-own-compare:*[1]"/>
      <xsl:variable name="vFoldFun" 
           select="document('')/*/maximum-pick-bigger:*[1]"/>
   
      <xsl:if test="$pList">
         <xsl:variable name="vCMPFun" select="$pCMPFun |
$vdfCMPFun[not($pCMPFun)]"/>
         <xsl:variable name="vFuncList">
            <xsl:copy-of select="$vFoldFun"/> <!-- Pick Bigger -->
            <xsl:copy-of select="$vCMPFun"/>  <!-- Compare -->
         </xsl:variable>

         <xsl:call-template name="foldl">
            <xsl:with-param name="pFunc" select="msxsl:node-set($vFuncList)/*"/>
            <xsl:with-param name="pList" select="$pList"/>
            <xsl:with-param name="pA0" select="$pList[1]"/>
         </xsl:call-template>
      </xsl:if>
    </xsl:template>

    <xsl:template name="pickBigger" match="maximum-pick-bigger:*">
         <xsl:param name="arg0"/>
         <xsl:param name="arg1"/>
         <xsl:param name="arg2"/>

         <xsl:variable name="vIsGreater">
           <xsl:apply-templates select="$arg0">
             <xsl:with-param name="arg1" select="$arg1"/>
             <xsl:with-param name="arg2" select="$arg2"/>
           </xsl:apply-templates>
         </xsl:variable>
         <xsl:choose>
           <xsl:when test="$vIsGreater = 1">
             <xsl:copy-of select="$arg1"/>
           </xsl:when>
           <xsl:otherwise>
             <xsl:copy-of select="$arg2"/>
           </xsl:otherwise>
         </xsl:choose>
    </xsl:template>

    <xsl:template name="isGreaterDefault" match="maximum-own-compare:*">
         <xsl:param name="arg1"/>
         <xsl:param name="arg2"/>

         <xsl:choose>
          <xsl:when test="$arg1 > $arg2">1</xsl:when>
          <xsl:otherwise>0</xsl:otherwise>
         </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

This returns a copy of the "maximum" node. The caller may provide as parameters
their own function (template reference to a template) that returns 1 if the node
passed as "arg1" to it is the "bigger" of the two nodes. In case the compared values
are numbers, no reference to an isGreater template needs to be provided and a
default internal one is used.

The XSLT implementation of foldl is available at:

http://sources.redhat.com/ml/xsl-list/2001-11/msg00214.html

Cheers,
Dimitre Novatchev.






__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com

 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.