Re: [xsl] current node attribute as predicate

Subject: Re: [xsl] current node attribute as predicate
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 25 Oct 2002 11:49:12 +0100

<xsl:template match="purchase">
		<xsl:variable name="ItemText">
			<xsl:value-of select="@item" />
		</xsl:variable>
		<xsl:copy-of select="/inventory/item[@itemcode=$ItemText]"
/>
	</xsl:template>




<xsl:variable name="ItemText">
			<xsl:value-of select="@item" />
		</xsl:variable>

makes ItemText into a variable which is a result tree fragment containing
a root node containing a text node with the value of the item attribute.
It is less to write and more efficient for the system if you go
<xsl:variable name="ItemText" select="@item" />
which makes ItemText a variable containing the attribute node.

It is often clearer if you do use a variable, as you did:

<xsl:copy-of select="/inventory/item[@itemcode=$ItemText]"/>

however if you don't want to use the variable you can use

<xsl:copy-of select="/inventory/item[@itemcode=current()/@item]"/>



Note however that if you are doing a lot of this then you are repeatedly
searching the entire tree finding items with the right itemcode.
If so it would be more efficient to make a key (if you only do it once
it probably doen't make any difference whether you use a key or not)

To use a key you would do at the top level

<xsl:key name="items" match="item" use="@itemcode"/>

then in the template you can jump straight to the right item


<xsl:template match="purchase">
	<xsl:copy-of select="key('items',@item)"/>
</xsl:template>


David

_____________________________________________________________________
This message has been checked for all known viruses by Star Internet
delivered through the MessageLabs Virus Scanning Service. For further
information visit http://www.star.net.uk/stats.asp or alternatively call
Star Internet for details on the Virus Scanning Service.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread