Re: Back to Basics: XPath and Context Node

Subject: Re: Back to Basics: XPath and Context Node
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 9 May 2000 09:35:39 +0100 (BST)
		< . . . .std. header stuff and PI . . .>
		<xsl:template match="/">
			<xsl:apply-templates />
		<xsl:template>

		<xsl:template match="child::para or child::*">
			---some stuff ---
		</xsl:template>
	
	The context node for my child:: matches is the root "/", yes? and
	my location path is essentially redundant b/c child::* includes 
	all para's anyway, obviating the "or," right?

This question appears confused (and the syntax is wrong:-)
child::para or child::*
is a boolean valued expression which is true if the context node has
a child and false otherwise. So it is equivalent to boolean(*)

You can't use such an expression as match pattern, so the second
template above is a syntax error, and probably should be

<xsl:template match="*">
			---some stuff ---
</xsl:template>

The context node for the (implied) select expression in the
apply-templates in the first expression is, as you say, the root node.
But the context for the selection does not affect which templates match
the selected nodes.

That is

if, from the root node, you do
<xsl:apply-templates select="para"/>
The context node is the root node and that determines which nodes
are selected (which in this case is just the para element(s)
which are child(ren) of the root.

However you may select the same set of nodes by using a different
select expression from a different context node, eg
select="/para" would select this same node set whatever the context node
is. and select="../../para" would select the same node set if the
context node happened to be a grandchild of the root.

However the nodeset was selected in apply-templates select=... the
_same_ set of templates would match as 

<xsl:template match="para">

matches any para node which is the child of anything (which in this case
is all para nodes) template matching is always with reference to nodes
in the source tree, not to the node set returned by the select
expression.

This distinction is a bit hard to see in the case of a simple match like
para but consider

match="xxx/para" that matches any para element that is a child of an xxx
element, but the xxx element just has to be in the source, it doesn't
have to be in the node set selected by xsl:apply-templates select="....


David


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


Current Thread