XPath Tester

Evaluate XPath expressions on sample HTML or XML.

Back to all tools on ToolForge

More in Developer Tools





Result


About XPath Tester

This XPath tester evaluates XPath expressions against XML or HTML documents using the browser's native DOMParser and XPathEvaluator APIs. It provides immediate feedback on XPath queries, showing matched nodes and their content.

It is useful for web scraping preparation, XML parsing workflows, testing XPath expressions before embedding in code, debugging node selection logic, and learning XPath syntax through hands-on experimentation.

What is XPath?

XPath (XML Path Language) is a W3C-standard query language for selecting nodes from XML documents. It uses path-like expressions to navigate document trees, similar to filesystem paths.

// Select all item elements
//item

// Select item with id="1"
//item[@id="1"]

// Select title inside section
//section/title

// Select first item
//item[1]

// Select items containing text "Apple"
//item[contains(text(),"Apple")]

XPath Axis Types

Axis Expression Description
Child root/item Direct children (default axis)
Descendant //item All descendants at any level
Parent item/.. Parent node
Attribute item/@id Attribute values
Following-sibling item/following-sibling::item Siblings after current node
Preceding-sibling item/preceding-sibling::item Siblings before current node

Common XPath Predicates

Predicates filter node sets using square brackets:

Predicate Meaning
[1] First node in set
[last()] Last node in set
[position()=2] Second node in set
[@id] Nodes with id attribute
[@id="x"] Nodes where id equals "x"
[text()="x"] Nodes with text content "x"
[contains(., "x")] Nodes containing text "x"
[starts-with(@id, "x")] Nodes where id starts with "x"

XPath Functions

// Node functions
count(//item)           // Count nodes
position()              // Current position
last()                  // Last position

// String functions
contains(., "text")     // Contains substring
starts-with(@id, "x")   // Starts with prefix
concat(@a, @b)          // Concatenate strings
string-length()         // String length

// Boolean functions
not(@id)                // Negation
boolean(//item)         // Convert to boolean

// Number functions
number(@price)          // Convert to number
sum(//price)            // Sum values

Common Use Cases

XML vs HTML Parsing

This tool parses input as XML using DOMParser with "text/xml" mode:

Handling Namespaces

XML namespaces complicate XPath because elements must match both local name and namespace URI:


//ns:element


//*[local-name()='element']


//*[local-name()='element' and namespace-uri()='http://example.com']

Frequently Asked Questions

What is XPath and how does it work?
XPath (XML Path Language) is a query language for selecting nodes from XML documents. It uses path-like expressions to navigate the document tree, similar to file system paths. XPath is supported by XSLT, XQuery, and many programming languages including JavaScript, Python, and Java.
What is the difference between XPath 1.0 and XPath 2.0?
XPath 1.0 is a W3C Recommendation from 1999 with basic node selection and string/number functions. XPath 2.0 (2007) adds sequence support, more data types, better string handling, and conditional expressions. This tool uses the browser's native XPath evaluator which typically supports XPath 3.0.
Can I use XPath on HTML documents?
Yes, but HTML must be well-formed XML (XHTML) for reliable XPath evaluation. The browser's DOMParser can parse HTML, but malformed HTML may produce unexpected results. For scraping real-world HTML, consider using tools like BeautifulSoup (Python) or Cheerio (Node.js) that handle tag soup.
What are common XPath axes and operators?
Common axes: child (default), descendant (//), parent (..), ancestor, following-sibling, preceding-sibling. Common predicates: [1] (first), [last()] (last), [@attr] (has attribute), [text()='x'] (text match). Functions: count(), contains(), starts-with(), position().
Why does my XPath return no results?
Common causes: XML namespaces not declared, case-sensitive element name mismatch, using absolute path when relative is needed, incorrect predicate syntax, or the target nodes don't exist in the document structure. Check the parsed document tree to verify element names and hierarchy.
How do I handle XML namespaces in XPath?
Namespaces complicate XPath because prefixed elements must match the namespace URI, not just the prefix. In JavaScript, you need to use the local-name() function: /*[local-name()='root' and namespace-uri()='http://example.com']. Full namespace support requires a namespace resolver function.