Skip to content
UoL CS Notes

XPath Basics

COMP207 Lectures

XPath allows us to write queries that return a set of value or nodes from an XML document:

  • Values
  • Nodes
    • Document Node
      • The whole document.
    • Element Node
      • Includes the root.
    • Attributes
      • Inside the tags of elements.

Path Expressions

They take the following format:

\[/E_1/E_2/E_3/\ldots/E_n\]

This would select all nodes reachable from the root by following the edges in the order of the path.

The result is returned in the document order.

Relative Path Expressions

You can miss out the first slash to evaluate relative to another node.

Attributes

The path expressions can be extended so that we can return attribute values:

/students/student/@name

Wildcards

A wildcard * represents any tag name or attribute name:

/students/student/module/@*

This would return all attributes of the module elements.