xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas B. Passin" <tpas...@mitretek.org>
Subject Re: XSLT Question
Date Wed, 18 Apr 2001 16:08:07 GMT
Malcolm Hawke had a stylesheet question.

> Using XSLT, I want to produce HTML that displays the drivers in order by
position and with the fastest lap speed of any driver highlighted somehow
(by '*' below), eg. output like
> Pos Driver Fastest Speed (kmh)
> 1 M Hakinnen 280
> 2 D Coulthard 285*
> 3 R Schumacher 277
> It's easy enough to sort by position, but how can I (efficiently!)
determine which is the fastest lap speed while preserving the required order
by position???

Here is a stylesheet that does it.  First, it sorts the drivers by their
fastest times in descending order.   Within that loop, the first node is the
fastest, and has position()=1.  When position()=1, the stylesheet calls
apply-templates with a parameter whose value is the "pos" attribute of the
first node, i.e., the fasted time.

In the apply-templates loop, if that parameter matches the "pos' attribute,
the item is marked.  It's simpler than it sounds.  For simplicity, I only
display the position and speed, but obviously you can change the stylesheet
to display all the elements, and sort into some other order if you want.
The key point is that this method lets you create a flag that you can pass
to another template.  Here is the stylesheet:

<xsl:stylesheet version="1.0"
<xsl:output method='html'/>

<xsl:template match='/race'>
  <xsl:for-each select='driver'>
     <xsl:sort select='fastest' order='descending'/>
     <xsl:if test='position()=1'>
         <xsl:apply-templates select='/race/driver'>
             <xsl:with-param name='pos' select='@pos'/> <!-- Here is the
marker -->

<xsl:template match='driver'>
 <xsl:param name='pos'/>
 <xsl:if test='@pos=$pos'>FASTEST: </xsl:if> <!-- This marks the fastest
driver -->
     Position <xsl:value-of select='@pos'/>: <xsl:value-of


Here are the results:

Position 1: 280
Position 3: 277
FASTEST: Position 2: 285


Tom P

In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org

View raw message