📖 Guide
XPath — Complete Reference
Quick reference for XPath expressions, axes, predicates, and functions for XML/HTML querying.
58 commands across 8 categories
Basic Syntax
| Command | Description |
|---|---|
/root/child | Absolute path from root |
//elemente.g. //div | Select all matching elements anywhere |
. | Current node |
.. | Parent of current node |
@attre.g. //a/@href | Select an attribute |
//* | Select all elements in the document |
//element[@attr]e.g. //input[@type] | Elements with a specific attribute |
//element[@attr="value"]e.g. //input[@type="text"] | Elements where attribute equals value |
Axes
| Command | Description |
|---|---|
child::nodee.g. child::div | Select children (default axis) |
parent::node | Select parent element |
ancestor::nodee.g. ancestor::form | Select all ancestors up to root |
descendant::nodee.g. descendant::span | Select all descendants |
following-sibling::node | All siblings after current node |
preceding-sibling::node | All siblings before current node |
following::node | Everything after current node in document order |
self::node | Current node itself |
Predicates
| Command | Description |
|---|---|
//ul/li[1] | First li child (1-indexed) |
//ul/li[last()] | Last li child |
//ul/li[last()-1] | Second to last child |
//ul/li[position()<3] | First two children |
//div[@class="active"] | Elements with exact attribute match |
//item[@price>10] | Numeric comparison in predicate |
//div[@class and @id] | Elements with multiple attributes |
//a[contains(@href, "example")] | Attribute contains substring |
Functions
| Command | Description |
|---|---|
count(//li) | Count matching nodes |
sum(//item/@price) | Sum numeric attribute values |
position() | Position of current node in context |
last() | Index of last node in context |
name() | Tag name of current node |
text()e.g. //p[text()="Hello"] | Text content of node |
not(expr)e.g. //div[not(@class)] | Boolean negation |
boolean(expr) | Convert to boolean |
String Functions
| Command | Description |
|---|---|
contains(str, "sub")e.g. //div[contains(@class, "active")] | Check if string contains substring |
starts-with(str, "prefix")e.g. //a[starts-with(@href, "https")] | Check if string starts with prefix |
string-length(str)e.g. //input[string-length(@value)>0] | Get string length |
concat(str1, str2) | Concatenate strings |
substring(str, start, len) | Extract substring (1-indexed) |
normalize-space(str) | Trim and collapse whitespace |
translate(str, from, to)e.g. translate("abc", "abc", "ABC") | Character-by-character replacement |
Node Tests
| Command | Description |
|---|---|
node() | Match any node (element, text, etc.) |
text() | Match text nodes only |
comment() | Match comment nodes |
processing-instruction() | Match processing instructions |
*e.g. //div/* | Match any element node |
@*e.g. //div[@*] | Match any attribute |
Operators
| Command | Description |
|---|---|
|e.g. //h1 | //h2 | //h3 | Union — combine two node sets |
ande.g. //input[@type="text" and @required] | Logical AND |
ore.g. //input[@type="text" or @type="email"] | Logical OR |
= != < > <= >= | Comparison operators |
+ - * div mod | Arithmetic operators (div not /) |
Common Patterns
| Command | Description |
|---|---|
//*[contains(@class, "btn")] | Find elements by partial class name |
//a[contains(text(), 'Click')] | Find links by visible text |
//table//tr[td] | All data rows in any table (skip header rows) |
(//div)[1] | First div in document (parentheses matter) |
//input[not(@type="hidden")] | All visible inputs |
//*[@id][1] | First element with an ID |
//div[count(p)>2] | Divs with more than 2 paragraph children |
//ul[li[3]] | Unordered lists with at least 3 items |
📖 Free, searchable command reference. Bookmark this page for quick access.