How to test an XPath expression
- Paste your XML, open a file or use the sample product catalog.
- Type an XPath expression or click one of the examples. The result updates as you type, and Enter evaluates it immediately.
- Read the result: the number of matching nodes with their type and content, or the number, string or boolean that the expression returns.
XPath cheat sheet
| Expression | Selects |
|---|---|
/catalog | The root element catalog |
//product | Every product element, at any depth |
/catalog/product/name | The name of each product, step by step from the root |
//product/@id | The id attribute of every product |
//product[@category='books'] | Products whose category attribute is books |
//product[price > 20] | Products with a price above 20, compared as numbers |
//product[contains(name, 'USB')] | Products whose name contains “USB” (case-sensitive) |
//product[not(tags)] | Products without a tags child |
(//product)[1] | The first product in the document |
//product[1] | The first product inside each parent element |
//product[last()] | The last product inside each parent element |
//name/text() | The text nodes inside name |
//price/.. | The parent element of each price |
//*[local-name()='product'] | product elements in any namespace |
count(//product) | The number of products |
sum(//price) | The total of all prices |
string(//product[1]/name) | The first product name as a string |
Namespaces in XPath
XPath 1.0 has no default namespace. In a document with xmlns="…", such as a sitemap or an NF-e invoice, //url finds nothing, because the elements belong to that namespace. The tester therefore maps the default namespace to the prefix x: (or d: if x is already taken), so //x:url/x:loc works. Prefixes declared in the document, such as cfdi: in a CFDI invoice, can be used as written. In tools without a namespace setup, //*[local-name()='url'] works everywhere.
Frequently asked questions
Which XPath version does the tester support?
XPath 1.0, the version built into every browser and most XML libraries. Functions from XPath 2.0 and later, such as matches(), lower-case() or string-join(), are not available.
Why does my XPath return nothing?
The most common causes are a default namespace (use the x: prefix shown under the examples), different capitalization – XPath is case-sensitive – and positions, which start at 1, not 0.
How many results are shown?
All matches are counted, and the first 500 are listed with their node type and content.
Is my XML uploaded?
No. The XML is parsed and queried in your browser. Only a URL you load is fetched through our server.