📐 XPath Evaluator

📄 XML Document

📐 XPath Expression

ℹ Tips

  • Use //tag for descendants anywhere; /root/tag from the document root.
  • [@attr='value'] filters by attribute; [position()=1] by index (1-based).
  • count(...), name(...), string(...), boolean(...) change the result type.
  • Default namespaces need a bound prefix in the expression — bare names will not match.

📤 Result

{{ primaryResult }}
Result type: {{ resultType || '—' }}
Match count: {{ resultType === 'node-set' ? matchCount : '—' }}

🔎 Matches

  1. {{ idx + 1 }}. {{ match }}
No matches yet — run an XPath expression that returns a node-set.
⚠️

ℹ️ About XPath Evaluator

What this tool does

The XPath Evaluator runs an XPath 1.0 expression against an XML document and reports the result. Paste your XML on the left, type the XPath expression below it, and press Evaluate. The tool reports the result type (node-set, number, string, or boolean) and lists every matching node along with its serialized form.

How it works (technical details)

  • Pure client-side. Parsing and evaluation both run inside your browser. No XML, no expression, and no result is sent over the network.
  • Parser. Input is parsed with DOMParser in application/xml mode. The browser does not throw on malformed input; instead it returns a document containing a <parsererror> element, which this tool detects and surfaces verbatim.
  • Evaluator. document.evaluate runs with XPathResult.ANY_TYPE, so the expression's natural result type is preserved. Node-sets are iterated via iterateNext() (or snapshotItem as a fallback) and each match is rendered as outerHTML for elements, name="value" for attributes, and text content for everything else.
  • Namespaces. Every xmlns and xmlns:prefix declaration in the parsed document is collected, and a resolver is passed to document.evaluate so prefixed expressions resolve. Note: XPath 1.0 cannot match unprefixed names in a default namespace — bind a prefix in your XPath expression for elements in a default namespace.

XPath syntax — quick reference

  • //book — every book element anywhere in the document.
  • //book[price<10] — every book whose price child is numerically less than 10.
  • //*[@id='foo'] — every element whose id attribute equals foo.
  • count(//book) — the number of book elements (returns a number, not a node-set).
  • name(//book[1]) — the qualified name of the first book element (returns a string).
  • //book/@isbn — every isbn attribute on a book element.
  • boolean(//book) — true if at least one book element exists.

Use cases

  • Configuration mining. Pull values out of pom.xml, Spring beans, Ant scripts, or SOAP envelopes without writing throwaway code.
  • Vendor responses. Inspect SOAP/REST-XML responses while you debug an integration.
  • RSS/Atom feeds. Filter a feed down to items that match a title pattern or category.
  • Sitemaps. Pull every <loc> URL out of a sitemap.xml for audit purposes.
  • Learning XPath. Iterate quickly on expressions against a sample document and see the result type change as you adjust the query.

Privacy note

This evaluator runs entirely in your browser using built-in DOMParser and document.evaluate APIs. No XML payload, no XPath expression, and no result is sent to a server. There is no API endpoint for this tool because Node has no first-party equivalent for these two primitives.

Limits and gotchas

  • XPath 1.0 only. document.evaluate implements XPath 1.0. Features added in XPath 2.0/3.0/3.1 (string functions like matches(), sequence types, FLWOR, schema awareness) are not available here.
  • Default-namespace gotcha. An XML document with xmlns="http://..." as a default namespace requires a prefixed XPath expression — e.g. //ns:book after binding ns in the document. The resolver picks up declared prefixes automatically; bare element names will not match.
  • HTML. This tool parses as XML, not HTML. Tag soup or unclosed tags will produce a parsererror. For HTML evaluation, query the live DOM or pre-process to XHTML first.
  • External entities. Browser DOMParser does not resolve external DTD references for security reasons, so entity expansion is limited to the five predefined XML entities.

References

Ready