Utility Coder
Back to Blog
utility

Xpath Tester

Andy Pham
xpath tester, xpath tester, xpath tester online

Ready to try Xpath Tester?

Access the free online tool now - no registration required

Open Tool

Xpath Tester - Complete Guide

XPath tester evaluates XPath expressions against XML documents to query and navigate XML trees. XPath is a powerful query language for selecting nodes, filtering by attributes, traversing relationships, and extracting data from XML.

How It Works

Parse XML into DOM tree, then evaluate XPath expression. Navigate using axes (child, parent, ancestor, following-sibling), predicates ([condition]), functions (text(), contains(), count()). Return matching node set or value.

Technical Details

XPath 1.0 is widely supported, 2.0/3.0 add more features. Node types: element, attribute, text, comment, processing-instruction. Axes: child, parent, ancestor, descendant, following, preceding, self. Return types: node-set, string, number, boolean.

Common Use Cases

  1. XML Parsing: Extract specific elements from XML.
    • Example: //book[@category="tech"]/title
  2. Web Scraping: Select HTML elements by structure.
    • Example: //div[@class="content"]//p
  3. XSLT Development: Test selectors before using in stylesheets.
  4. Config Files: Query XML configuration files.
    • Example: /config/database/host

Code Examples

XPath Evaluation

// Browser XPath API
const xml = `<library>
  <book category="tech" available="true">
    <title>JavaScript Guide</title>
    <author>John</author>
    <price>29.99</price>
  </book>
  <book category="fiction" available="false">
    <title>The Story</title>
    <author>Jane</author>
    <price>19.99</price>
  </book>
</library>`;

const parser = new DOMParser();
const doc = parser.parseFromString(xml, 'text/xml');

// Evaluate XPath
function xpath(expr, context = doc) {
  const result = doc.evaluate(
    expr, context, null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
  );
  const nodes = [];
  for (let i = 0; i < result.snapshotLength; i++) {
    nodes.push(result.snapshotItem(i));
  }
  return nodes;
}

// Examples
xpath('//book/title');                           // All titles
xpath('//book[@category="tech"]');               // Tech books
xpath('//book[price>20]/title');                 // Titles of books over $20
xpath('//book[@available="true"]/author');       // Authors of available books
xpath('count(//book)');                          // Number of books (use NUMBER_TYPE)

// Get text content
xpath('//book[1]/title')[0].textContent;         // "JavaScript Guide"

Browser native XPath evaluation. For Node.js, use xmldom with xpath package.

Tips & Best Practices

  • Use // for descendant search, / for direct children
  • Predicates filter: [@attr="val"], [position], [condition]
  • text() gets text content, . gets current node
  • Combine with and/or for complex conditions
  • Register namespaces when working with namespaced XML

Common Mistakes to Avoid

  • Using / when // is needed for deep search
  • Forgetting namespace handling
  • Wrong quote nesting in predicates
  • Assuming 1-based vs 0-based indexing (XPath is 1-based)
  • Not handling multiple matches

When to Use This Tool

Use XPath tester when parsing XML, testing XPath expressions for XSLT, web scraping HTML, or learning XPath syntax.

Frequently Asked Questions

What's the difference between / and //?

/ selects direct children only. // selects descendants at any depth (descendant-or-self). //title finds all title elements anywhere, /root/title finds only direct children of root.

How do predicates work?

Predicates filter nodes: [@attr="value"] by attribute, [position()=1] or [1] by position, [text()="content"] by text. Combine with and/or. [price>10 and @available="true"]

Can I select by text content?

Yes. //p[text()="exact"] for exact match, //p[contains(text(),"partial")] for partial, //p[starts-with(text(),"prefix")] for prefix. normalize-space() handles whitespace.

How do I handle namespaces?

Register namespace prefixes in your XPath engine, then use them: //ns:element. Default namespaces require explicit prefix. Without registration, use local-name(): //*[local-name()="element"]

Get Started

Ready to try Xpath Tester? Use the tool now - it's free, fast, and works right in your browser.


Last updated: June 27, 2026

Start using Xpath Tester

Free, fast, and privacy-focused - try it now

Launch Tool