XML technologies / XSLT / Introduction to XSLT 2.0 / Getting started with XSLT and XPath / Getting started with XPath: path expressions / The path expression – relative and absolute

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers)

Although the XPath expression here is simply gruss, it has the same overall meaning as the compound expression /gruss, because it must be considered relative to the context /. Thus, gruss is a relative expression.

Since a relative expression has the effect of being concatenated with its current context, its effect varies with the evaluation context it refers to. Relative path expressions are always context-dependent.

Where there are relative expressions, there are also absolute ones. In fact, you have already seen one without realizing it: /gruss is such an absolute expression. Absolute expressions always start with a slash »/«. They always produce the same result regardless of the current context: absolute path expressions are document-dependent.

It is now clear why the instruction could not produce a result in the second case: it was given the wrong path. The path to the text in <gruss> leads from the document node through the element <hallo>. The complete expression should therefore be understood as follows:

/hallo/gruss

Since the document node is already the context, for xsl:value-of it is sufficient to use the relative expression hallo/gruss:

<xsl:value-of select="hallo/gruss"/>

An absolute expression can always be used as well. In this case, therefore,

<xsl:value-of select="/hallo/gruss"/>

would work just as well.

In practice, you will usually use the relative expression, precisely to avoid constantly dealing with the potentially changing absolute position of the information sought.

   

back to top