RE: [xsl] Document function (was Getting an error with a variable)

Subject: RE: [xsl] Document function (was Getting an error with a variable)
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 29 Jul 2006 10:26:31 +0100
> This is a snippet of code. Am I on the right 
> track or is there a more efficient way?
> 
>   <xsl:for-each
> select="/legs/competition/leg/Competitor[../../@name=$paramVal1]">
>   <tr>
>     <td><xsl:value-of select="@no"/></td>
> 	<xsl:variable name="cNum">
> 	    <xsl:value-of select="@no"/>
> 	</xsl:variable>

Never do this. Write

<xsl:variable name="cNum" select="@no"/>

There's no need to construct a new result tree fragment when you only want a
reference to a node. (And no need for three lines of code when one would do
- XSLT is verbose enough without making it worse).

>     <td><xsl:value-of
> select="document('EntryList.xml')/entrylist/competition/entry/
> @driverSurname
> [../@no=$cNum]"/>
>     <td><xsl:value-of
> select="document('EntryList.xml')/entrylist/competition/entry/
> @coDriverSurna
> me[../@no=$cNum]"/>

You're less reliant on the optimizer if you define a variable:

<xsl:variable name="entry" 
 
select="document('EntryList.xml')/entrylist/competition/entry[@no=$cNum]"/>

then select="$entry/@driverSurname" and select="$entry/@codriverSurname"

Note also, predicates can appear on steps in a path expression other than
the last. You can write

document('EntryList.xml')/entrylist/competition/entry[@no=$cNum]/@driverSurn
ame

Michael Kay
http://www.saxonica.com/

Current Thread