XPath Tester
Run an XPath 1.0 expression on the XML you paste and see the matching elements, attributes or the result of count()
Your data stays with you. Conversion happens inside the browser; nothing is sent to a server.
Did this tool do the job?
Thanks, your feedback came through.
How it works
XPath is the expression language for picking elements, attributes or text out of an XML document; Selenium tests, XSLT stylesheets and programs that read data from XML feeds all use it. Paste your XML into the left pane and type the expression; the result on the right updates as you type. On the sample document, //book[price<10]/title returns the titles of the two books under 10: Pride and Prejudice and Nineteen Eighty-Four. Each matching node is written on its own line: an element as XML, an attribute as name="value", and text chosen with text() as plain text. An expression that returns a number (count(//book) gives 3), a string (string(//book[1]/author) gives Jane Austen) or true/false (//year=1813 gives true) shows a single value, and the note below names the result type. If the document declares prefixes such as xmlns:cbc, write the same prefix in the expression (//cbc:ID). If the XML is broken, the line and column of the problem are shown.
This tool is also known as xpath tester, xpath online, xpath evaluator, xpath examples, test xpath.
What is XPath?
XPath (XML Path Language) is the W3C standard for selecting nodes in an XML document with a path expression. A path such as /bookstore/book selects elements, @category selects attributes, text() selects text, and a condition in square brackets narrows the result. XSLT, XQuery, Selenium and many XML libraries use XPath; browsers support version 1.0, published in 1999.
What is XML namespace?
A namespace ties element names to a vocabulary identified by an address: the declaration xmlns:cbc="urn:..." says that elements with the cbc: prefix belong to that address. SOAP messages and UBL invoices use many namespaces. In XPath, an element matches only if both its name and its namespace match, which is why prefixes also appear in the expression.
What is the difference between XPath and a CSS selector?
A CSS selector (div.product > a) selects only elements and only moves down the document; it is fast enough for styling in the browser and for finding elements in Selenium. XPath can also select attribute and text nodes, step up to the parent (..) or to siblings, filter by text (//a[text()='Add to cart']) and calculate with count() and sum(). Use XPath to pull values out of XML data or to find an element by its text; use a CSS selector when you only need to pick elements in HTML, because it is shorter.
Common XPath expressions (with the sample XML)
You can try every expression below on the bookstore sample on this page. A single slash looks one level down, a double slash at every depth; square brackets hold a condition, and @ means an attribute. Numbers in XPath use a decimal point (8.99).
- //title → the three title elements
- //book[@category='classic']/author → the authors of the two classics
- //book[price<10]/title/text() → only the title text, without tags
- /bookstore/book[last()]/title → the title of the last book (Nineteen Eighty-Four)
- count(//book) → 3; sum(//price) → 31.44
- //title/@lang → the lang attributes (lang="en")
Namespaces and honest limits
Prefixed namespaces (xmlns:cbc="...") are read from the declarations in the document, and you use the same prefix in the expression. A default namespace without a prefix (xmlns="...") is a well-known XPath 1.0 trap: //book finds nothing. The tool binds the default namespace to the prefix d:, so write //d:book or //*[local-name()='book']. When nothing matches, the note below reminds you of this.
The tool runs XPath 1.0, the same version browsers support. XPath 2.0 and 3.1 functions (matches, lower-case, tokenize, date functions) are not available; if you use one, the tool names the missing function. The pasted text must be well-formed XML: ordinary HTML with an unclosed <br> or unquoted attributes gives an error. To try XPath on a live web page, use $x("//a") in your browser's developer tools console.
Frequently asked questions
How do I test an XPath expression?
Paste your XML into the left pane on this page and type the expression into the XPath box. Matching elements show up on the right at once, and the line below says how many nodes matched. If the expression has a typo or uses a function that XPath 1.0 lacks, the tool says so.
What is the difference between // and /?
A single slash looks at the direct children of the current node: /bookstore/book selects only the book elements right under the root. A double slash searches at every depth: //author finds every author element wherever it is in the document. If you do not know the structure well, start with //.
Why does XPath return nothing on XML with a namespace?
If the document declares a default namespace with xmlns="...", the XPath 1.0 rules say an unprefixed //book does not see elements in that namespace. In this tool write //d:book, or use //*[local-name()='book'], which ignores namespaces. For prefixed namespaces such as xmlns:cbc, write the prefix exactly as it appears in the document.
Is my XML sent to a server?
No. Reading the XML and running the XPath expression both happen in your browser, so you can safely test a document that contains invoice or customer data.