xml
  1. xml-xquery-xpath

XML XQuery XPath

XPath expressions can be used in XQuery to select nodes and values from an XML document.

Syntax

The basic syntax of an XPath expression is:

xpath-expression ::= step / step / ...
step ::= axis-name::node-test [predicate] / axis-name::node-test [predicate] / ...
axis-name ::= 'ancestor' | 'ancestor-or-self' | 'attribute' | 'child' | 'descendant' | 'descendant-or-self' | 'following' | 'following-sibling' | 'namespace' | 'parent' | 'preceding' | 'preceding-sibling' | 'self'
node-test ::= element-test | attribute-test | text-test | processing-instruction-test | node()
predicate ::= '[' xpath-expression ']'
element-test ::= qname | '*' | 'comment()' | 'processing-instruction()'
attribute-test ::= '@' qname | attribute() | 'attribute()'
text-test ::= 'text()' | 'node()'
processing-instruction-test ::= 'processing-instruction()' | 'processing-instruction(name)'
qname ::= ncname ':' ncname | ncname
ncname ::= [A-Za-z_][A-Za-z0-9_.-]*

Example

Consider the following XML document:

<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <price>44.95</price>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <price>5.95</price>
  </book>
</catalog>

To select the title of the first book, we can use the following XPath expression:

/catalog/book[1]/title

This will return the <title> element with the value "XML Developer's Guide".

Output

The output of an XPath expression can be a value or a set of nodes. In the case of the previous example, the output would be a single node:

<title>XML Developer's Guide</title>

Explanation

XPath expressions consist of one or more steps separated by the / operator. Each step consists of an axis, a node test, and optional predicates. An axis is a direction of the node selection, and can be one of the following:

  • ancestor: selects all ancestors of the context node.
  • ancestor-or-self: selects the context node and all its ancestors.
  • attribute: selects all attributes of the context node.
  • child: selects all child nodes of the context node.
  • descendant: selects all descendants of the context node.
  • descendant-or-self: selects the context node and all its descendants.
  • following: selects all nodes that come after the context node in document order.
  • following-sibling: selects all siblings that come after the context node.
  • namespace: selects all namespace nodes of the context node.
  • parent: selects the parent of the context node.
  • preceding: selects all nodes that come before the context node in document order.
  • preceding-sibling: selects all siblings that come before the context node.
  • self: selects the context node.

A node test is a condition that a node must fulfill to be selected. It can be one of the following:

  • element-test: matches an element node with the given name, any name (*), a comment (comment()), or a processing instruction (processing-instruction() or processing-instruction(name)).
  • attribute-test: matches an attribute node with the given name (@name), or any attribute (attribute() or attribute(name)).
  • text-test: matches a text node (text()) or any node (node()).
  • processing-instruction-test: matches a processing instruction node (processing-instruction()), or a processing instruction node with the given name (processing-instruction(name)).

Predicates are expressions in square brackets ([]) that can filter the selected nodes.

Use

XPath expressions can be used in XQuery to extract data from an XML document, modify it, or generate a new document. They can also be used in XPath-based APIs and in XSLT stylesheets.

Important Points

  • XPath expressions can select nodes or values from an XML document.
  • They consist of one or more steps, which include an axis, a node test, and optional predicates.
  • XPath expressions can be used in XQuery, XPath-based APIs, and XSLT stylesheets.

Summary

In this tutorial, you learned about XPath expressions in XQuery, and how they can be used to select nodes and values from an XML document. XPath expressions consist of steps, which include an axis, a node test, and optional predicates. They can be used in XQuery, XPath-based APIs, and XSLT stylesheets.

Published on: