Generating HTML Documents

   



Finding the Maximum Value of A Set of Numbers

Now we can determine which chapter in the book is the longest. The XSL stylesheet in maxLength1.xsl contains an example of how to compute the maximum value in a list of values.

Listing A.7 maxLength1.xsl

start example
<?xml version="1.0"?> <xsl:stylesheet version="1.0"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!--- the 'max' template computes the maximum -->  <xsl:variable name="maxLength">    <xsl:call-template name="max">       <xsl:with-param name="chapters"            select="/book/chapters/chapter"/>    </xsl:call-template>  </xsl:variable> <xsl:template match="/"> Max length = <xsl:value-of select="$maxLength"/> </xsl:template> <xsl:template name="max">   <xsl:param name="chapters"/>     <xsl:choose>       <xsl:when test="$chapters">         <xsl:variable name="firstItem"                       select="$chapters[1]/length"/>         <xsl:variable name="rest-of-list">           <xsl:call-template name="max">              <xsl:with-param name="chapters"                   select="$chapters[position()!=1]"/>           </xsl:call-template>         </xsl:variable>         <xsl:choose>           <xsl:when test="$firstItem &gt; $rest-of-list">              <xsl:value-of select="$firstItem"/>           </xsl:when>           <xsl:otherwise>              <xsl:value-of select="$rest-of-list"/>           </xsl:otherwise>         </xsl:choose>       </xsl:when>       <xsl:otherwise>0</xsl:otherwise>     </xsl:choose>   </xsl:template> </xsl:stylesheet>
end example

Remarks

The first time you see this type of XSL code, you might feel slightly overwhelmed by its appearance. Unlike LISP and Snobol, this sort of calculation does not have a simple, compact syntax in XSL. The key idea involves recursively invoking the max template with the all-but-first list of chapters (in LISP it's called the CDR of the list) as shown below:

    <xsl:variable name="rest-of-list">       <xsl:call-template name="max">         <xsl:with-param name="chapters"              select="$chapters[position()!=1]"/>       </xsl:call-template>     </xsl:variable>

The final invocation of the max template will (by necessity) contain an empty list of chapters, and the template returns the value 0 by means of this code fragment near the bottom of the stylesheet:

<xsl:otherwise>0</xsl:otherwise>

Notice how the code is executed: the 'top' of the template will be executed until the end of the list of chapters is reached, then the 'bottom' is executed (which returns 0), and finally the 'middle' part of the template is executed, which contains the comparison that determines the maximum chapter length, as listed below:

<xsl:choose>   <xsl:when test="$firstItem &gt; $rest-of-list">      <xsl:value-of select="$firstItem"/>   </xsl:when>   <xsl:otherwise>     <xsl:value-of select="$rest-of-list"/>   </xsl:otherwise> </xsl:choose>

You can easily modify the max template by using &lt; in order to create a min template that computes the minimum value of a set of numbers.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net