Xerobit

XPath Tester: Evaluate XML Path Expressions

Paste an XML document and type an XPath expression to see the matching nodes, their paths and any text, number or boolean result. This xpath tester implements XPath 1.0 with all axes and functions, checks the XML for well-formedness and runs entirely in your browser.

Well-formed XML. A DOCTYPE is skipped but never processed.

Result

Matched 2 node(s)

1) /catalog[1]/book[1]/title[1]/text()[1]
   Example Book

2) /catalog[1]/book[2]/title[1]/text()[1]
   Another Book
  • Result type
    Node-set
  • Matches
    2

How to use the xpath tester

  1. Paste or type a well-formed XML document into the XML Document field.
  2. Enter an XPath expression such as //book/title or count(//item) in the XPath Expression field.
  3. The xpath tester evaluates the expression instantly as you type.
  4. Review the matched nodes, extracted text values or computed numbers in the result area.

How the XPath evaluation works

This xpath tester parses your expression into a syntax tree and evaluates it against the XML document tree using the rules of XPath 1.0. All axes except namespace:: are available, together with node tests, predicates, unions, the arithmetic, comparison and boolean operators, and the 27 core functions: count(), sum(), string(), concat(), contains(), starts-with(), substring(), translate(), normalize-space(), position(), last(), not(), lang(), round() and the rest.

A relative expression such as catalog/book starts at the document node, the parent of the root element. A node-set result is listed in document order with the path of each node and its value. A string, number or boolean result is shown as plain text.

Common XPath expressions

Frequently used patterns, all of which work in the tester:

ExpressionDescription
/root/childDirect children named child under the root element
//itemEvery item element anywhere in the document
//book[@lang='en']Books whose lang attribute equals en
//book[price>20]/title/text()Titles of books priced above 20
//node/text()The text content of matching nodes
count(//div)The number of div elements
//li[last()]The last li inside each list
(//li)[last()]The last li in the whole document
//@hrefAll href attributes
//b/following-sibling::*[1]The element right after each b
//a[not(@id)]Anchors without an id attribute

Namespaces and the default namespace trap

In XPath 1.0 an unprefixed name only matches elements that have no namespace. If the root of your XML declares xmlns="urn:example", then //entry matches nothing, even though the elements look unprefixed. The tester detects this case and shows a hint under a zero-match result.

There are two ways to write the query. The portable one ignores the namespace: //*[local-name()='entry']. The precise one uses a prefix that is declared somewhere in the XML, for example //a:entry when the document contains xmlns:a="urn:example". The tester resolves prefixes in your expression from the declarations found in the document, and reports an error if the prefix is unknown.

Supported XML and limits

The XML parser checks well-formedness: matching start and end tags, a single root element, quoted attribute values, no duplicate attributes, legal characters and legal character references, valid comments, processing instructions and CDATA sections. It resolves the five predefined entities and numeric references such as A and B, and handles namespace declarations.

A DOCTYPE is skipped without being processed. Entities declared in it are not expanded, so a document that uses one reports an undefined entity. This keeps the tool safe from entity expansion attacks and from external resources. Variables such as $name, the namespace:: axis and XPath 2.0 functions like lower-case() or matches() are not available, and id() returns an empty set because no DTD is read.

Input is limited to 1,000,000 characters of XML, 500 levels of element nesting and 4,000 characters of XPath. Very expensive expressions, such as following:: or preceding:: from every node of a large document, stop with an error instead of freezing the page.

Error reporting with line and column

When the XML is not well-formed, the tester stops at the first problem and reports its line and column. Typical messages are a mismatched end tag, an element that is never closed, an unescaped & or < in text, an attribute without quotes, or text after the root element. Escape a literal ampersand as &amp; and a literal less-than sign as &lt;.

Syntax errors in the expression are reported separately, with the position of the unexpected token. A missing bracket, an unknown function or a prefix that is not declared in the document each get their own message.

Practical use cases

Developers working with SOAP responses, RSS and Atom feeds, SVG files, Maven POMs or configuration formats often need one value out of a large XML file. An xpath tester lets you refine the expression before you paste it into code, a test assertion or a scraper.

Before querying, you can check the structure with the xml validator, tidy it with the xml formatter, or browse it with the xml viewer. To compare two versions of a document, use the xml diff tool.

Worked examples

ExampleInputResult
Select all titlesXML Document: <catalog><book><title>A</title></book><book><title>B</title></book></catalog>; XPath Expression: //title/text()Result type: Node-set; Matches: 2
Find by attributeXML Document: <items><item id="1">First</item><item id="2">Second</item></items>; XPath Expression: //item[@id='2']/text()Result type: Node-set; Matches: 1
Filter by numberXML Document: <catalog><book><title>A</title><price>29.99</price></book><book><title>B</title><price>15.00</price></book></catalog>; XPath Expression: //book[price>20]/title/text()Result type: Node-set; Matches: 1
Count elementsXML Document: <list><a/><a/><a/></list>; XPath Expression: count(//a)Result type: Number
Default namespaceXML Document: <feed xmlns="urn:x"><entry>1</entry><entry>2</entry></feed>; XPath Expression: //*[local-name()='entry']Result type: Node-set; Matches: 2

FAQ

What XPath version does this tester support?

XPath 1.0, including every axis except namespace::, predicates, unions, operators and the 27 core functions. XPath 2.0 and 3.0 features such as sequences, regular expressions and lower-case() are not included.

Is my XML data sent to a server?

No. The XML is parsed and the expression is evaluated locally in your browser. Your document and queries never leave your device.

Why does my query return nothing when the element is clearly there?

The most common cause is a default namespace. If the root has xmlns="...", unprefixed names do not match in XPath 1.0. Use //*[local-name()='name'] or a declared prefix. Names are also case-sensitive.

Why does my XML with a DOCTYPE not expand entities?

The DOCTYPE is skipped on purpose and its entity declarations are not processed, which avoids entity expansion attacks and external lookups. Replace custom entities with plain text before pasting.

Can I test XPath on HTML documents?

Only if the HTML is well-formed XML. Real HTML often has unclosed tags or unquoted attributes, which this tester reports as errors. Convert it to XHTML first, or use a browser console with document.evaluate.

What entities are recognized in the XML input?

The five predefined entities (amp, lt, gt, quot, apos) and numeric character references in decimal and hexadecimal form.

How do I select an attribute value?

Use the @ prefix. For example, //item/@id returns the id attribute of every item element, and //item[@id='2'] selects the item whose id is 2.

How do I get the second or last match?

Use a predicate. //li[2] is the second li of each parent, //li[last()] is the last li of each parent, and (//li)[last()] is the last li in the entire document.