Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current node vs. Context node in XSLT/XPath?

Tags:

xml

xslt

xpath

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?

like image 904
Paul A Jungwirth Avatar asked Jun 20 '09 19:06

Paul A Jungwirth


People also ask

What is the current node in XPath?

Current node is the node that the XPath processor is looking at when it begins evaluation of a query. In other words, the current node is the first context node that the XPath processor uses when it starts to execute the query. During evaluation of a query, the current node does not change.

What is a current node in XPath How can it be accessed?

The current node is only relevant if you are in an XSLT-scope; it refers to the node the current template is applied to and can be accessed using current() . For plain XPath (without XSLT), this function is not available and the current not neither accessible nor relevant.

Which node types are valid in XPath?

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.


2 Answers

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:

<a>     <b>         <c>foo<footnote fn="1"/></c>         <d>bar</d>     </b>     <b>         <c>baz</c>         <d>aak<footnote fn="2"/></d>     </b>     <b>         <c>eep</c>         <d>blech<footnote fn="2"/></d>     </b>     <footnote-message fn="1">Batteries not included.</footnote>     <footnote-message fn="2">Some assembly required.</footnote> </a> 

Now suppose you want to convert it to LaTeX like this:

foo\footnote{Batteries not included.} bar  baz aak\footnote{Some assembly required.}  eep blech\footnotemark[2] 

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:

<xsl:choose>     <xsl:when test="count(preceding::*[./@fn = current()/@fn]) = 0">\footnote{...}</xsl:when>     <xsl:otherwise>\footnotemark[...]</xsl:otherwise> </xsl:choose> 

Here we are comparing the context-node fn attribute (from the results of the preceding::* node-set) to the current-node fn 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.

like image 73
Paul A Jungwirth Avatar answered Sep 27 '22 00:09

Paul A Jungwirth


Context Node

The context node is part of the XPath evaluation context and varies with each location step:

step1 / step2 / step3 / ... 

where each step is

axis::node-test[predicate] 
  • Each step is evaluated with respect to the context nodes set by the preceding steps.
  • Each step then selects nodes that become the context node for following steps.
  • When evaluating predicate, the context node is the node along axis that has passed node-test.
  • The context node can be accessed as ..

Current Node

The current node () is part of the XSLT processing model:1

  • The current node is the node in the source XML document best matched by an XSLT template.
  • The current node becomes the starting context node for each XPath expression in the matched template.
  • The current node can be accessed as 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.

like image 45
kjhughes Avatar answered Sep 25 '22 00:09

kjhughes