ModernCalcs

XPath Validator & Tester

Test an XPath expression against an XML document and see exactly which nodes it matches, using your browser's native XPath engine.

Not available during SSR

XPath Validator: Test Expressions Against Real XML, Live

Writing an XPath expression against a large or unfamiliar XML document usually means trial and error. This tool lets you paste your actual XML and an XPath expression, and instantly see which nodes match — powered by the same document.evaluate() engine every browser uses natively, so results match what you'd get in real XML tooling.

Formula
doc.evaluate(expression, doc, null, XPathResult.ANY_TYPE, null)

Automatically detects whether the result is a node-set, string, number, or boolean.

Node-Set vs. Scalar Results

Most XPath expressions like //title return a node-set — a collection of matching elements, attributes, or text nodes. But expressions like count(//book) or //price > 15 return a single number or boolean instead. This tool detects which kind of result an expression produces and formats the output accordingly, rather than assuming everything is a list of nodes.

Common XPath Building Blocks

// selects nodes anywhere in the document regardless of their position, [ ] applies a predicate/filter (like [@category='fiction'] or [price > 15]), @ selects an attribute, and text() selects a node's text content rather than the element itself. Combining these covers the majority of real-world XPath queries.

Debugging 'Why Doesn't My XPath Match Anything'

The most common cause of zero matches is a namespace mismatch — many real-world XML documents declare a default namespace, and unprefixed XPath expressions won't match namespaced elements without an explicit namespace resolver. If an expression that looks correct returns nothing, check whether your XML declares an xmlns attribute.

Practical Examples

Selecting Filtered Elements

Finding books in a specific category.

  • 1.Expression: //book[@category='fiction']
  • 2.Matches: ...

Aggregating with a Function

Counting matching nodes instead of listing them.

  • 1.Expression: count(//book)
  • 2.Result (number): 2

Useful XPath Patterns

  • //tag: any element named 'tag', anywhere
  • //tag[@attr='value']: filter by attribute
  • //tag[position()=1] or [1]: first matching element
  • //tag/text(): the text content of matching elements
  • count(//tag): number of matches

Good Use Cases

  • Prototyping a scraper or XML parser's selectors before writing code
  • Debugging why a production XPath query returns unexpected results
  • Learning XPath syntax with immediate visual feedback
  • Validating that an XML document's structure matches expectations

Frequently Asked Questions

What XPath version does this support?

It uses the browser's built-in document.evaluate() engine, which implements XPath 1.0 — the version supported natively by all major browsers.

What happens if my XML is malformed?

The tool parses your XML with DOMParser first and reports a clear parsing error if the document isn't well-formed, before even attempting to evaluate the XPath expression.

What happens if my XPath expression is invalid?

The browser's evaluate() call throws a syntax error, which is caught and displayed directly — for example, an unmatched bracket or an invalid axis name.

Can I test expressions that return a number, string, or boolean instead of nodes?

Yes. Expressions like count(//book) or //book[1]/author/text() = 'Tolkien' return a number or boolean rather than a node set, and the tool detects and displays the correct result type automatically.

Does this evaluate XPath the same way a backend XML library would?

It uses the same XPath 1.0 specification that libraries like libxml2, lxml, or Java's javax.xml.xpath implement, so expressions that work here should behave the same in most XPath 1.0 environments — though some libraries add non-standard extension functions this tool won't recognize.

Is my XML data sent anywhere?

No. Parsing and evaluation both run through browser-native APIs (DOMParser and document.evaluate) entirely on your device.