XPath Syntax — Complete Guide with Examples
XPath uses path expressions to select nodes in XML documents. This guide covers every part of XPath 1.0 syntax: location paths, axes, node tests, predicates, and built-in functions.
XPath Tester
Test XPath expressions against XML data with real-time evaluation. Extract elements, filter by attributes, and navigate XML document structures.
XPath Reference
| Expression | Description |
|---|---|
/ | Root element |
//element | All matching elements anywhere |
./child | Direct child of context |
@attr | Attribute value |
[1] | First element (1-indexed) |
[last()] | Last element |
[position()<3] | First two elements |
[@attr='val'] | Filter by attribute |
[contains(., 'text')] | Contains text |
[starts-with(@id, 'x')] | Starts with |
text() | Text content |
node() | Any node |
count(//el) | Count elements |
sum(//el) | Sum of numeric values |
string-length(//el) | String length |
ancestor::el | Ancestor axis |
descendant::el | Descendant axis |
following-sibling::el | Following siblings |
parent::el | Parent axis |
el1 | el2 | Union of two node sets |
About XPath
XPath (XML Path Language) is a query language for selecting nodes from XML documents. It uses path expressions to navigate through elements, attributes, and text in an XML tree structure.
- Nodes — elements, attributes, text, comments, and the document itself
- Axes — define the direction of navigation (child, parent, ancestor, descendant, sibling)
- Predicates — filter nodes with conditions inside square brackets
- Functions — built-in string, number, and node functions (contains, count, sum, etc.)
XPath is used in XSLT, XQuery, web scraping (Selenium, Puppeteer), configuration parsing, and XML data extraction. This tool uses your browser's built-in XPath 1.0 engine — no data is sent over the network.
Location paths and steps
An XPath location path is a sequence of steps separated by /. Each step has three parts: an axis (direction to navigate), a node test (what to match), and zero or more predicates (filters). For example, /bookstore/book[1]/title is three steps: select the root bookstore, then its book children filtered to the first one, then the title child. A double slash // is shorthand for the descendant-or-self axis — //title selects all title elements at any depth.
// Absolute path from root
/bookstore/book/title
// Relative path — all titles anywhere
//title
// Current context
./author
// Parent
../priceAxes for navigation
XPath defines 13 axes for navigating the document tree. The most common are child:: (default, direct children), parent:: (parent node), ancestor:: (all ancestors up to root), descendant:: (all descendants), following-sibling:: and preceding-sibling:: (siblings), self:: (current node), and attribute:: (abbreviated as @). For example, //book/following-sibling::book selects all book elements that come after another book.
// All ancestors of a node
//title/ancestor::*
// Following siblings
//book[1]/following-sibling::book
// Attribute axis (@ shorthand)
//book/@category
// Descendant-or-self
//bookstore/descendant-or-self::titlePredicates and functions
Predicates filter node sets using conditions in square brackets. They can test position ([1], [last()], [position()<3]), attribute values ([@lang='en']), element values ([price>50]), and use 27+ built-in functions. String functions: contains(), starts-with(), string-length(), substring(), normalize-space(), translate(), concat(). Number functions: count(), sum(), floor(), ceiling(), round(). Boolean functions: not(), true(), false(). Node functions: name(), local-name(), namespace-uri().
// Position predicates
//book[1]
//book[last()]
//book[position() <= 2]
// Attribute predicates
//book[@category='programming']
// String functions
//book[contains(title, 'Code')]
//book[starts-with(author, 'Robert')]
// Numeric functions
count(//book)
sum(//book/price)Frequently Asked Questions
What is the difference between / and // in XPath?
A single slash / selects from the root or direct children of the current context node. A double slash // is shorthand for /descendant-or-self::node()/ — it selects matching nodes at any depth in the document. For example, /bookstore/book only matches book elements that are direct children of the root bookstore, while //book matches book elements anywhere in the document tree.
How do I select the nth element in XPath?
Use a position predicate in square brackets: //book[1] selects the first book, //book[2] the second, and //book[last()] the last. XPath positions are 1-indexed (not 0-indexed like most programming languages). You can also use ranges: //book[position() >= 2 and position() <= 4] selects books 2 through 4.
Does XPath support regular expressions?
XPath 1.0 does not support regular expressions. For pattern matching, use contains(), starts-with(), and substring(). XPath 2.0 adds the matches() function for regex, but browser built-in evaluators only support XPath 1.0. For regex needs, consider extracting nodes with XPath and then filtering with JavaScript regex.
Related Inspect Tools
Diff Checker
Compare two texts and see differences highlighted
Cron Expression Parser
Parse cron schedules into plain English with next run times
Word & Character Counter
Count words, characters, sentences, and estimate reading time
Chmod Calculator
Calculate Unix file permissions with an interactive permission matrix