RE: [xsl] How to: Reference to the current Nodeset while processing through another one

Subject: RE: [xsl] How to: Reference to the current Nodeset while processing through another one
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 29 Sep 2004 10:16:20 +0100
My immediate answer to the question in your subject line is "use variables".

Now I see:

> I want to get all other "Value" items, having the same 
> id. I want to
> get them without using a referenc variable.

and my immediate reaction is: why? It's like saying you want to eat
spaghetti, and you want to do it without using a fork.

> 
> I tried:
> <xsl:for-each select="//Value[@id = Value/@id]">
> 	<!-- Do something -->
> </xsl:for-each>
> 
> But this returned all Values having a attribut "id".

I'm surprised this would return anything. It asks for all Value elements
that have an @id attribute and that have a child Value element with the same
value of @id.
> 
> If I declare a variable with the value of "id"
> <xsl:variable name="v_id">
> 	<xsl:value-of select="@id"/>
> </xsl:variable>

It's better to write this as

<xsl:variable name="v_id" select="@id"/>

but in this case either will work.

> and process though the other nodes by using the variable it works:
> <xsl:for-each select="//Value[@id = $v_id]">
> 	<!-- Do something -->
> </xsl:for-each>
> 
> In this case I get all "Value" nodes having a "id" of "123".
> But how can I do this without using a variable? 

If you really hate variables that much, you can often get by with current(),
which is in effect a built-in variable set to the node that was the current
node at the outermost level of the XPath expression. So you can write

<xsl:for-each select="//Value[@id = current()/@id]">

Incidentally, this would be much faster with keys.

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

Current Thread