In XSLT, what is the difference between the "current node" and the "context node"? You can find both terms used here: http://www.w3.org/TR/xslt.
When would you use one or the other? How do you refer to each?
In XSLT, what is the difference between the "current node" and the "context node"? You can find both terms used here: http://www.w3.org/TR/xslt.
When would you use one or the other? How do you refer to each?
Context Node
The context node is part of the XPath evaluation context and varies with each location step:
where each
step
ispredicate
, the context node is the node alongaxis
that has passednode-test
..
.Current Node
The current node () is part of the XSLT processing model:1
current()
within XPath predicates.1Although insignificant to understanding the basic difference between context node and current node, note that in XSLT 2.0 the description of the evaluation context has been changed. The concepts of current node and current node list have been replaced by the XPath concepts of context item, context position, and context size.
The current node is whatever the template is currently operating on. Normally this happens to also be the context node, but the context node has special meaning within a nested XPath expression (the part in square brackets). There, it refers to whatever node is currently being tested for a match. Hence, the context node changes within the XPath expression, but not the current node.
The context node can be abbreviated with a dot (
.
) or sometimes left out entirely. This is probably a little confusing, because outside of a nested expression, a dot signifies the current node. (In that case the current node happens to be the context node, so one might say that it is the current node only proximately, and it is more properly called the context node. But even the spec calls it the current node here.)Since a dot gives you the context node, in a nested XPath expression the user needs a way to refer back to the current node, the one being processed by the current template. You can do this via the
current()
function.Distinguishing these two is useful in some cases. For instance, suppose you have some XML like this:
Now suppose you want to convert it to LaTeX like this:
The trick is the tell whether a footnote has already been used or not. If this is the first time you've encountered the footnote, you want to write a
\footnote
command; otherwise you want to write a\footnotemark
command. You could use XSL code like this:Here we are comparing the context-node
fn
attribute (from the results of thepreceding::*
node-set) to the current-nodefn
attribute. (You don't actually have to say./@fn
; you could just say@fn
.)So in short, the context node leaves you inside the XPath predicate; the current node reaches outside the predicate, back to the node being processed by the current template.