xsl-list
[Top] [All Lists]

Re: current node attribute as predicate

2002-10-25 03:49:12


<xsl:template match="purchase">
                <xsl:variable name="ItemText">
                        <xsl:value-of select="@item" />
                </xsl:variable>
                <xsl:copy-of select="/inventory/item[(_at_)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[(_at_)itemcode=$ItemText]"/>

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

<xsl:copy-of select="/inventory/item[(_at_)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



<Prev in Thread] Current Thread [Next in Thread>