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]
Other format: [Raw text]

RE: max value


Charly wrote:
> Is there a way to get the max value .
> 
> something that would look like and returns "11"
> 
> <xsl:template match="values">
>       <xsl:value-of select="max(value)" />
> </xsl:template>
> 
> <values>
>    <value>7</value>
>    <value>11</value>
>    <value>8</value>
>    <value>4</value>
> </values>

A recursive template could do this.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:template match="/values" >
  <xsl:call-template name="getMaxValue">
   <xsl:with-param name="count" select="count(value)" />
  </xsl:call-template>
 </xsl:template>
 
 <!-- Start of the recursive template -->
 <xsl:template name="getMaxValue">
  <xsl:param name="count"/>
  <xsl:param name="maxValue" />
  <xsl:variable name="curValue" select="value[position() = $count]" />
  <xsl:choose>
   <xsl:when test="$count != 1">
    <xsl:variable name="newMaxValue">
     <!-- If the current value is greater than the previous maxValue, set the newMaxValue to the 'new max value' -->
     <xsl:choose>
      <xsl:when test="$curValue &gt; $maxValue">
       <xsl:value-of select="$curValue"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="$maxValue"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
    <xsl:call-template name="getMaxValue">
     <xsl:with-param name="count" select="$count - 1"/>
     <xsl:with-param name="maxValue" select="$newMaxValue"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$maxValue" />
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

HTH,
Joshua

------------------------------------------------------------------------------
This message and any attachment is confidential and may be privileged or otherwise protected from disclosure.  If you have received it by mistake please let us know by reply and then delete it from your system; you should not copy the message or disclose its contents to anyone.





 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]