Cheatsheet: XPath
Last updated 2026-06-21
Paths and node tests
Select the document root.
Select car elements that are direct children of the cars root element.
Select car elements anywhere in the document.
Select the current context node.
Select the parent of the current context node.
Use a wildcard to select any child element under cars.
Use union to combine results from multiple paths.
Attributes and predicates
Select elements by attribute value.
Select the make attributes from all car elements.
Select elements that have a specific attribute regardless of value.
Select the first car child for each cars element.
Select the last car child for each cars element.
Select cars with price greater than 30000.
Combine predicate conditions with and.
Axes
Select all descendant model elements below each car.
Select ancestor cars elements for matching model nodes.
Select the parent element of each model node.
Select following sibling elements after a title.
Select preceding sibling elements before a price.
Select all attributes with the attribute axis.
Functions
Match exact text content of an element.
Select elements whose text contains a substring.
Select elements whose attribute starts with a prefix.
Normalize whitespace before comparing text.
Select by numeric position using position().
Count matching nodes.
See also: