Skip to content
uencoder
TASK

Use XPath to query XML documents

XPath navigates XML trees with path expressions, predicates, and functions. Learn absolute and relative paths, wildcards, and common functions.

  1. 1

    XPath expressions select nodes from an XML tree: /root/child selects all child elements, //element selects anywhere, [@attr="value"] filters by attribute, text() extracts text content, * is a wildcard. XPath 1.0 is universally supported; XPath 2.0+ adds typing and more functions.

  2. 2
    JSONPath Query →

    Use JSONPath (the JSON equivalent of XPath) to query JSON converted from XML. First convert XML to JSON, then use JSONPath expressions like $.root.child[?(@.attr=="value")]. This is a two-step alternative to direct XPath.

  3. 3
    XML Formatter →

    Format the XML to understand its structure before writing XPath expressions. Indentation reveals the hierarchy, making it easier to construct correct paths. Verify your XPath by checking which nodes it selects.

  4. 4

    Common XPath patterns: /bookstore/book[1] (first book), /bookstore/book[price>35] (filter by child value), //title (all titles anywhere), count(//book) (count books), //book/@category (attribute values). Use predicates [...] for filtering and functions like text(), contains(), starts-with().

Frequently asked questions

XPath vs CSS selectors?
CSS selectors work on HTML/XML elements (simpler, limited). XPath is more powerful: can select by text content, use functions, navigate to parent/ancestor, and handle namespaces. Use CSS selectors for simple HTML queries; use XPath for complex XML or when CSS is insufficient.
XPath 1.0 vs 2.0 vs 3.0?
XPath 1.0 is universally supported (browsers, libxml). XPath 2.0 adds sequences, richer type system, more functions — supported by Saxon. XPath 3.0 adds dynamic evaluation. For web/browser use, stick with 1.0. For server-side XML processing with Saxon, use 2.0+.

Workflows that solve this

Learn more

Want to understand why?

Related tools