Skip to main content

XPath Expression

XPath defines a pattern or path expression for selecting nodes or sets of nodes in an XML document.

These patterns are used by XSLT to perform transformations.

XPath Specifications

Nodes

XPath views an XML document as a tree of nodes. This tree starts at a single "root" and branches out to elements, their attributes, and their text content.

Consider this simple XML document:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J. K. Rowling</author>
</book>
</bookstore>

The XPath specification defines seven types of nodes that make up this tree structure:

  • Root: The very top-level node of the document tree (conceptually, it's the parent of the root element, like <bookstore>).
  • Element: The most common node type; it represents an XML element (e.g., <book>, <title>).
  • Attribute: Represents an attribute of an element (e.g., category="cooking").
  • Text: Represents the text content inside an element (e.g., "Everyday Italian").
  • Namespace: Represents a namespace declaration.
  • Processing Instruction: Represents a processing instruction (e.g., <?xml-stylesheet ... ?>).
  • Comment: Represents a comment in the XML (e.g., <!-- My Comment -->).

Path Selection Expression

XPath uses a path expression to select node or a list of nodes from an XML document.

Following is the list of useful paths and expression to select any node or list of nodes from an XML document.

ExpressionDescription
nodenameIt is used to select all nodes with the given name nodename
/It specifies that selection starts from the root node.
//It specifies that selection starts from the current node that match the selection.
.Select the current node.
..Select the parent of the current node.
@Selects attributes.
note

You will discover more in next chapters.