Docs / Queries
XPath queries
Last updated:
XPath is a query language for XML. The XPath panel selects nodes, attributes, and text from an XML document, including RSS and Atom feeds. The queries run on your device. The XPath panel is a Pro feature. Free users get one free try of it per day.
The panel reads XPath queries with the browser's native XPath engine. That engine implements XPath 1.0. This page shows how to open the panel, run a query, read the result, and work with namespaces.
Open the XPath panel
The XPath panel is available when an XML file is open.
Open the panel in one of two ways:
- In the toolbar, select
XPath(in the Transform group). - Open the Command Palette with
Cmd+K(Mac) orCtrl+K(Windows, Linux), then runQuery / Transform.
The panel title is XPath Query. A Pro badge marks the toolbar button for free users.
Run a query
Type an XPath expression in the input field. Select the run button, or press Enter.
The panel lists Examples on the side. Select one to load it. A short XPath Reference shows common forms.
The panel keeps your last query and result for the session, so they return when you reopen the panel. Prettify does not save queries to disk.
The XPath panel reads data only. It does not change the document.
Read the result
The result depends on what the query returns:
- A node set lists each match. An element shows as its XML. An attribute shows as
name="value". A text node shows its text. - A number, such as the output of
count(), shows as a number. - A string, such as the output of
string(), shows as text. - A boolean shows as
trueorfalse.
The panel shows the match count above the list. It shows No matches found when a valid query matches nothing.
Examples
Every example below uses this document:
<catalog>
<book isbn="123">
<title>XPath Basics</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book isbn="456">
<title>Advanced XML</title>
<author>Jane Roe</author>
<price>39.99</price>
</book>
</catalog>
Select every book element:
//book
Select books that have an isbn attribute:
//book[@isbn]
Read the title text of each book:
//book/title/text()
Select books with a price above 30:
/catalog/book[price>30]
Find an author by text content:
//author[contains(text(), "John")]
Read an attribute value:
//book/@isbn
Count the books:
count(//book)
Select the first and the last book:
//book[1]
//book[last()]
Match on the start of the text:
//title[starts-with(., "Adv")]
Sum the prices:
sum(//price)
Functions
The engine supports the XPath 1.0 function library. Common functions include:
| Group | Functions |
|---|---|
| Node set | count(), position(), last(), name(), local-name() |
| String | contains(), starts-with(), substring(), substring-before(), substring-after(), string-length(), normalize-space(), translate(), concat(), string() |
| Boolean | not(), true(), false(), boolean() |
| Number | sum(), round(), floor(), ceiling(), number() |
Namespaces
XPath 1.0 has no default namespace. An unprefixed name in a query matches only elements that have no namespace. A document that declares xmlns="..." on its root puts every element in a namespace, so an unprefixed query returns nothing.
The panel solves this for you. It reads the namespace declarations from the document. It lists the available prefixes under Namespaces:. For the default namespace, it adds a prefix named ns (or ns1, ns2, and so on when the document already uses ns).
Use the prefix to select elements in the default namespace. For a document with a default namespace, write:
//ns:title
A prefix that the document declares with xmlns:prefix is already usable. For example, //atom:entry works when the feed binds the atom prefix.
If a query uses a prefix the document does not declare, the panel shows an error and lists the known prefixes.
Related pages
Try it online
Tool pages for this topic. Each one runs in your browser.