Skip to main content

XPath Absolute and Relative Paths

In XPath, there are two types of paths used specify the location of a node in XML documents.

These are absolute path and relative path.

Absolute Path

An absolute path starts with root node or with /.

For example:

  • /bookstore/book: It will select book nodes within class root node.
<p><xsl:for-each select="/bookstore/book"></p>
  • /bookstore/book/author: It will select author of book node within class root node.
<p><xsl:value-of select="/bookstore/book/author"/></p>

Relative Path

If location path starts with the node that we've selected then it is a relative path.

For example:

  • title: It selects title related to book nodes.
<p><xsl:value-of select="title"/></p>

Selecting Several Paths

Using the | operator in an XPath expression you can select several paths.

In the table below we have listed some example of path expressions and the result of the expressions:

Path ExpressionResult
//book/title | //book/priceSelects all the title AND price elements of all book elements
//title | //priceSelects all the title AND price elements in the document
/bookstore/book/title | //priceSelects all the title elements of the book element of the bookstore element AND all the price elements in the document
note

You can learn more about Node Operators and Functions in our XPath Node Operators and Functions chapter.