RE: [xsl] getting the context out of a variable

Subject: RE: [xsl] getting the context out of a variable
From: "Michael Kay" <mhkay@xxxxxxxxxxxx>
Date: Tue, 18 Sep 2001 13:37:46 +0100
> another problem, which I have, is getting the context out of
> a variable.

I think what you're saying is, you have a variable containing a node-set
(let's say its a single node), and you want to find out where in the source
tree that node came from? (Your terminology is confusing, though, because
"context" in XPath doesn't mean the position of a node in the source tree,
it's to do with the way in which the node was selected by the stylesheet.)
> An example:
> <xsl:variable name="tree" select="document(tree.xml)/root"/>
> <xsl:template match="/">
>     <xsl:apply-templates select="$tree/node"/>
> </xsl:template>
> <xsl:template match="node">
>     <!-- the context is here now in the variable -->
>     <xsl:if test="@name = /page/html/node/@name">

This only tests whether the string-value of the @name attribute is the same
as the string-value of some node in the node-set "/page/html/node/@name". I
imagine what you are trying to do is to test whether the context node (whose
name is <node>) matches the XSLT pattern "/page/html/node". Perhaps you are
also trying to test which document it comes from.

The simplest way to test the ancestry of the context node is by turning the
path upside down:


You can also use this technique to test whether the node belongs to a
particular document:

<xsl:if test="generate-id(ancestor::node()[last()]) =

Note the use of generate-id() here, because you want to compare the identity
of two nodes, not their string-values or names.

I hope this is relevant to your problem.

Mike Kay

 XSL-List info and archive:

Current Thread