XSLT - XPath expressions

Understanding XPath expressions is crucial for navigating XML in XSLT. One of the key distinctions is absolute vs relative paths


1. XPath Expressions

  • XPath is a language to locate nodes in an XML document.

  • An XPath expression specifies a path to a node or set of nodes in the XML tree.

  • Paths can be absolute or relative, depending on whether you start from the root or from the current node.


2. Absolute XPath

  • Definition: Starts from the root node of the XML document.

  • Syntax: Begins with a /.

  • Behavior: Always follows the same path from the root, regardless of the current context.

Example XML:

<library>
  <book>
    <title>1984</title>
  </book>
  <book>
    <title>Brave New World</title>
  </book>
</library>

Absolute XPath Examples:

XPath Meaning
/library Selects the <library> element at the root
/library/book Selects all <book> elements directly under <library>
/library/book/title Selects all <title> elements under <book> elements under <library>

Note: Absolute paths are fixed and always start from / (the document root).


3. Relative XPath

  • Definition: Starts from the current context node, not necessarily the root.

  • Syntax: Begins without / or with ./ or ../.

  • Behavior: Depends on the node where you are currently applying the XPath (like inside a template or xsl:for-each).

Relative XPath Examples:

  • book/title → Selects <title> child elements of the current node called <book>

  • ./title → Same as above; explicitly starts from the current node

  • ../book → Selects <book> elements from the parent of the current node

  • @id → Selects the id attribute of the current node


4. Example in XSLT

XML:

<library>
  <book id="b1">
    <title>1984</title>
    <author>George Orwell</author>
  </book>
  <book id="b2">
    <title>Brave New World</title>
    <author>Aldous Huxley</author>
  </book>
</library>

XSLT Using Absolute Path:

<xsl:template match="/">
  <h2>All Titles</h2>
  <ul>
    <xsl:for-each select="/library/book">
      <li><xsl:value-of select="title"/></li>
    </xsl:for-each>
  </ul>
</xsl:template>

XSLT Using Relative Path (inside a template for <book>):

<xsl:template match="book">
  <li>
    <xsl:value-of select="title"/> by <xsl:value-of select="author"/>
  </li>
</xsl:template>

<xsl:template match="/library">
  <h2>All Books</h2>
  <ul>
    <xsl:apply-templates select="book"/>
  </ul>
</xsl:template>

Explanation:

  • Absolute path /library/book always starts from the root.

  • Relative path book inside /library template depends on the current node context, which is <library>.


5. Key Differences

Feature Absolute Path Relative Path
Starts from Root node (/) Current node
Syntax Begins with / Begins with element name, ./, ../, or @
Context dependent No Yes
Example /library/book/title book/title, ./title, ../book

In short:

  • Absolute XPath → fixed path from the root of XML.

  • Relative XPath → path relative to the current node, flexible inside templates or loops.

  • Relative paths make XSLT more modular and reusable.