XPath Tester

XPath (XML Path Language) selects nodes from XML documents using path expressions. Key syntax: // selects anywhere in document (//element), / is path separator, @ selects attributes (//book/@id), [] is predicate filter (//book[@id="1"]), text() selects text content. Examples: //p selects all paragraphs, //book[@category="fiction"]/title gets fiction book titles, //employee[dept="Engineering"]/name/text() returns employee names by department.

Test XPath expressions against XML documents online. Paste any XML and run XPath 1.0 queries to select elements, attributes, or text nodes. Results shown instantly. Supports element selection, attribute filtering, text() nodes, predicates, and all standard XPath axes.

Works OfflineDark ModeNo Ads

XML Document

XPath Expression

Presets

XPath Quick Reference

ExpressionDescription
text()Select text content of element
@attrSelect attribute value
//elementSelect element anywhere in document
/root/childSelect child of root (absolute path)
parent/child[1]First child element
element[@attr="val"]Element with attribute value
contains(@attr, "val")Attribute contains value
count(//element)Count matching elements
normalize-space(text())Trim whitespace from text
not(@attr)Element without attribute

How to Use

  1. 1

    Paste your XML

    Enter or paste a valid XML document in the XML input area. The tool validates XML before running the query and shows parse errors if any.

  2. 2

    Enter an XPath expression

    Type your XPath query in the expression field. Examples: //p selects all paragraphs, //book[@id="1"]/title gets a specific book title, //name/text() extracts text content.

  3. 3

    View results

    Matching nodes are shown as a numbered list. Elements are serialized as XML, attributes show their value, text nodes show their content. Result count is displayed in the header.

  4. 4

    Copy results

    Click Copy to get all results as newline-separated text. Use the presets to quickly test common XPath patterns.

Frequently Asked Questions

What is XPath?
XPath (XML Path Language) is a query language for selecting nodes from an XML document. It uses path expressions to navigate the document tree, similar to file system paths. XPath 1.0 is supported in all browsers. Example paths: //element selects all occurrences of element, /root/child selects a specific child, //book[@category="fiction"] selects books with a specific attribute. XPath is used in XML processing, web scraping, XSLT transformations, and automated testing (Selenium).
What is the difference between // and / in XPath?
/ (single slash) is an absolute path or child separator. Starting with / means starting from the root. /catalog/book means the book element that is a direct child of catalog at the root. // (double slash) selects nodes anywhere in the document, at any depth. //book selects all book elements regardless of where they appear. //book/title selects all title elements that are children of book elements anywhere in the document. Use // for flexible matching, / for precise path navigation.
How do I select elements with a specific attribute value?
Use a predicate in square brackets: //book[@id="bk101"] selects book elements where the id attribute equals "bk101". Common attribute predicates: [@attr] (attribute exists), [@attr="value"] (exact match), [contains(@attr, "value")] (contains), [starts-with(@attr, "value")] (starts with), [@attr > 10] (numeric comparison). To get just the attribute value, use //book/@id. To select elements without an attribute: //book[not(@discontinued)].
How do I select text content with XPath?
Use text() to select the text node of an element: //title/text() returns the text content of all title elements. To filter by text: //title[text()="Moby Dick"] selects titles with that exact text. To use normalize-space to trim whitespace: //title[normalize-space(text())="Moby Dick"]. To get all text in a subtree: string(//section) returns all text content combined. Note that text() only selects direct text children, not text inside nested elements. Use string() to get all descendant text.
What are XPath axes?
XPath axes define the direction of navigation from the context node. Common axes: child:: (default, direct children), parent:: (the parent node), ancestor:: (all ancestors), descendant:: (all descendants at any depth), following-sibling:: (siblings after), preceding-sibling:: (siblings before), attribute:: (same as @), self:: (the node itself). Example: //book/following-sibling::book selects book elements that come after other books. //* selects all elements. //node() selects all nodes including text nodes and attributes.
How is XPath used in automated testing?
XPath is widely used in Selenium WebDriver to locate HTML elements: driver.findElement(By.xpath("//input[@id='username']")). Tips for robust selectors: prefer @id over position-based paths, use contains() for partial matches (//button[contains(text(), "Submit")]), avoid deep paths that break on HTML changes, prefer @data-testid attributes when available. XPath also supports count() for verifying element counts, and conditional selection: //tr[td[@class="active"]]. CSS selectors are often faster in browsers but XPath is more expressive for complex selections.

Related Tools