XPath tester

Test an XPath 1.0 expression on your XML and see the matching nodes, attributes, text or the number, string or boolean it returns.

loading XPath tester
About this tool

What is an XPath tester?

XPath is the language for pointing at parts of an XML document. A path such as /catalog/book/title walks down the tree, and predicates in square brackets narrow the choice: //book[price < 10] means every book element, at any depth, whose price is under 10. The same expressions turn up in XSLT, in Selenium and scraping scripts, in SOAP and RSS tooling, in Java and .NET code and in many configuration formats.

This tester evaluates an expression against the XML you paste and shows what comes back. It uses your browser's own XPath engine, so the language is XPath 1.0, which is what browsers implement. XPath 2.0 and 3.1 features, such as for expressions, matches(), replace(), sequences and if then else, are not available. If your expression works here, it will also work in any XPath 1.0 engine.

The result depends on what the expression returns. When it selects nodes, you see each one on its own line: an element as XML, an attribute as name="value", a text node as plain text. When it returns a number, a string or a boolean, such as count(//book), the tool shows the value and says which kind it is. The XML and the expression stay in your browser and are not sent anywhere.

How to test an XPath expression

  1. Paste your XML into the XML field. The "Example" button loads a small catalog with three books.
  2. Type the expression in the XPath field.
  3. Read the result on the right. For nodes, the counter says how many were found. For other results, a label tells you whether it is a number, a string or a boolean.
  4. Copy the result or download it as a text file.
  5. If the document is not well-formed XML, the message says so and points to the line and column. If the expression is wrong, the message comes from the browser's XPath engine.

Examples

The examples use the catalog from the Example button: three book elements with an id and a category attribute, and title, author and price children. The results match what xmllint --xpath prints for the same document.

Select elements with a condition

//book[price < 10]/title
<title>Sayings of the Century</title>
<title>Moby Dick</title>

Attributes and text

//book/@id returns attributes as name="value":

id="b1"
id="b2"
id="b3"

//book[@category="fiction"][last()]/title/text() returns only the text of the last fiction title, Moby Dick. /catalog/book[position() <= 2]/@id returns id="b1" and id="b2". //price[. > 9] returns <price>12.99</price>.

Numbers, strings and booleans

ExpressionResultKind
count(//book)3number
sum(//price)30.93number
string(//book[2]/author)Evelyn Waughstring
boolean(//nothing)falseboolean

A prefix declared on the root

For <r xmlns:p="urn:x"><p:a>1</p:a></r>, the expression //p:a/text() returns 1. Prefixes declared on the root element are known to the tool.

Common mistakes

  • Using an XPath 2.0 function. matches(), replace() or tokenize() fail in XPath 1.0. Use contains(), starts-with(), substring-before() and translate() instead.
  • Forgetting the default namespace. In XPath 1.0 an unprefixed name never matches an element in a default namespace (xmlns="..."). Use //*[local-name()="book"], or declare a prefix on the root element and use it.
  • Expecting an error when nothing matches. A valid expression with no result returns zero nodes, and the tool says so.
  • Unbalanced brackets. //book[ is an invalid expression. The message names the problem.
  • XML that is not well-formed. An unclosed tag, such as <a><b></a>, stops the evaluation. The message says it is the XML that is wrong, not the expression. XML formatter helps you find the broken part.
  • Mixing up text and string. //title returns elements. //title/text() returns the text nodes inside them. string(//title) returns only the text of the first one.
  • Comparing text as numbers by accident. price < 10 converts the value to a number, so it works. Comparing two strings with < does not do what you might expect.

Frequently asked questions

Which XPath version does it support?

XPath 1.0, because the tool uses the engine built into the browser. XPath 2.0 and later are not supported.

Can I test CSS selectors or convert them?

This page tests XPath only. To translate a CSS selector, use the CSS to XPath converter.

Does it work on HTML?

The input is parsed as XML, so it must be well-formed. Real-world HTML often is not. For XML data, see also XML to JSON.

Can the expression change my XML?

No. XPath only selects. To change the structure, you need a stylesheet, which you can try in the XSLT tester.

Report a problem