XPath Cheatsheet - XML/HTML Selector Syntax Reference
Regex is the wrong tool for pulling nodes out of XML/HTML — XPath was built for it. This reference covers node paths, predicate filters, axis navigation, string/functions and wildcards, with the scraping cases you actually meet. Use it when scraping a page, querying an XML config or asserting in tests, instead of writing fragile regex that breaks on whitespace. After reading you write a selector that targets exactly the node you want, including positions and attributes.
Basic Paths 7
///.../bookstore/book//bookbook/titlePredicate Filtering 6
//book[1]//book[last()]//book[position()<3]//book[@lang="zh"]//book[price>35]//book[title]Axes 7
ancestor::divparent::node()child::titledescendant::pfollowing-sibling::divpreceding-sibling::h2self::aCommon Functions 8
contains(@class, "active")starts-with(text(), "Hello")normalize-space()string-length(text())count(//book)concat("a", "-", "b")substring("hello", 1, 3)translate("abc", "abc", "ABC")Wildcards & Operators 6
*@*node()//a | //b//div[@id="x" and @class="y"]//div[@class="a" or @class="b"]Tips
- // is much slower than /; prefer exact paths for large documents.
- contains(@class, 'x') matches composite classes like 'x y', more practical than exact match.
- XPath 1.0 has no regex; for regex matching use XPath 2.0 or combine with contains.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us