XPath Expressions

XPath is the query language for selecting nodes in XML documents. Used in XSLT, XSD identity constraints, Selenium/Playwright selectors, web scraping tools, and XML processing libraries.

Syntax

xml-fundamentals
/ — root or child separator
// — any descendant
. — current node
.. — parent node
@ — attribute prefix
[predicate] — filter expression

Example

xml-fundamentals
/catalog/book           — direct path
//title                 — any title element
@id                     — id attribute
//book[@genre="fiction"] — filtered
//book[1]               — first book
//book[last()]          — last book
//book[price > 15]      — comparison
count(//book)           — function
sum(//price)            — aggregation
//title/text()          — text content