xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Malcolm Hawke <malcolm_ha...@yahoo.com>
Subject XSLT Question
Date Thu, 19 Apr 2001 02:48:42 GMT

I am having a problem with the following. I am using the MSXML version 3 parser with Internet
explorer 5.0, but this may be a general enough question. I hope someone can give me some hints!

Say I have an xml file, the contents of which are:

      <driver pos="1">
         <name>Mika Hakinnen</name>
         <fastest units="kmh">280</fastest>

      <driver pos="3">
         <name>Ralf Schumacher</name>
         <fastest units="kmh">277</fastest>

      <driver pos="2">
         <name>David Coulthard</name>
         <fastest units="kmh">285</fastest>


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???

So far I have managed to do this by performing an initial sort by fastest lap and storing
the fastest speed using (hideous!) JavaScript with <msxml:script>. This is Microsoft
specific and I seek to avoid it. I haven't been able to use <xsl:variable> because of
scoping reasons (I need to access the value elsewhere in the XSL stylesheet).

Some of the stylesheet is included below. Please help!

I am a bit suprised that there doesn't seem to be an easier and more efficient way of doing
this, and while I am at it, doing something as easy as converting a string into lower case
(other than a huge <xsl:translate>) seems hard.

Would I be better off doing this sort of stuff outside of the stylesheet, eg. in vbscript/javascript
using DOM, and passing the fastest speed into the style sheet as an <xsl:parameter>?



<msxsl:script language="JavaScript" .... >
var fastest = 0;

function setFastest( f ) { fastest = f; return ''; }
function getFastest() { return fastest; }

<xsl:template match="/">

   <!-- determine the fastest speed and store -->
   <xsl:apply-templates select="race/driver" mode="fastest">
      <xsl:sort select="fastest" data-type="number" order-by="descending"/>

   <!-- display the list -->
   <xsl:apply-templates select="race/driver">
      <xsl:sort select="race/driver[@pos]" data-type="number" order-by="ascending"/>


<!-- store the fastest lap in the js variable for later reference -->
<xsl:template match="driver" mode="fastest">
   <xsl:if test="position() = 1">
      <xsl:value-of select="setFastest( number( fastest ) )"/>

<!-- display list of drivers in finishing order with the fastest speed highlighted -->
<xsl:template match="driver">
   <xsl:value-of select="pos"/>
   <xsl:value-of select="name"/>
   <xsl:value-of select="fastest"/>

<!-- append '*' if this is the fastest speed of the race -->
   <xsl:if test="select = getFastest()">*</xsl:if>

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