Skip to content
Buy me a coffee
Validators

XPath Tester / Evaluator

Test your XPath expressions against an XML document. Supports XPath functions (string(), number(), name(), string-length()…), namespaces and returns node-sets, strings, numbers or booleans.

0 characters · 0 lines
Option 2: Or upload your XML file
XPath 3.1 (fontoxpath). Namespace prefixes declared in the document can be used directly.
Ctrl + Enter
Try an expression

XPath examples

XPath (XML Path Language) selects nodes and computes values from an XML document. An expression is evaluated against the document and returns a sequence of items: nodes (elements, attributes, text, comments, processing instructions, the document itself) or atomic values (strings, numbers, booleans, dates…). The examples below use the sample document (a CD catalog); load it with the "Load sample" link and click an example chip to try it.

ExpressionResult
/catalog/cdAll cd elements that are children of the root catalog element.
//cdAll cd elements anywhere in the document (descendant-or-self axis).
//cd[1]The first cd child of each parent (positions are relative to the parent, not global).
(//cd)[1]The very first cd in document order.
//cd[last()]The last cd child of each parent.
//cd[position() < 3]The first two cd elements of each parent.
//cd[price > 10]/titleTitles of the CDs whose price is greater than 10.
//cd[@id] and //cd[not(@id)]CDs with, respectively without, an id attribute.
//cd[@id='2']/artistThe artist of the CD whose id is 2.
//cd/@idThe id attribute nodes of every CD.
//title/text()The text nodes inside every title.
//* and /catalog/*Every element / every child element of the root.
//cd[artist='Bob Dylan' or country='UK']Boolean operators and, or, not() in predicates.
count(//cd)The number of CDs (an xs:integer).
sum(//price), avg(//price), max(//year)Aggregates over the numeric values of the selected nodes.
string-length(//title[1])The length of the first title.
name(/*), local-name(//*[1])The (local) name of the root element.
//cd[contains(title, 'heart')]CDs whose title contains "heart" (case-sensitive).
//cd[starts-with(artist, 'Bo')]/titleTitles of the CDs whose artist starts with "Bo".
normalize-space(//cd[1]/artist)Trims and collapses whitespace in the artist name.
translate(//title[1], 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')Upper-cases the first title (XPath 1.0 way; upper-case() in XPath 2+).
concat(//cd[1]/title, ' — ', //cd[1]/artist)Concatenates strings.
substring-before(//price[1], '.')The integer part of the first price, as a string.
boolean(//cd[price > 100])false: converts the (empty) node-set to a boolean.
number(//price[1]) * 2Arithmetic on converted values.
//cd[year > 1985]/following-sibling::cd[1]/titleAxes: following-sibling, preceding-sibling, parent, ancestor, descendant, self.
//price[. > 10]/ancestor::cd/titleWalks up to the enclosing cd and down to its title.
//foo:cd/bar:yearElements in the namespaces bound to the foo and bar prefixes (see below).
//*[local-name()='cd']Every cd element regardless of namespace.
//comment(), //processing-instruction()Comment and processing-instruction nodes.
string-join(//title, ', ')XPath 2+: joins all titles into one string.
//cd/title ! upper-case(.)XPath 3: the simple map operator applies a function to every item.
for $c in //cd return concat($c/title, ' (', $c/year, ')')XPath 2+: iteration with for … return.
//cd[price = max(//price)]/titleThe most expensive CD(s).
distinct-values(//country)Unique country names.
if (count(//cd) > 3) then 'many' else 'few'Conditional expressions (XPath 2+).
[1, 2, 3], map { 'a': 1 }XPath 3.1 arrays and maps.

Namespaces

Every namespace prefix declared in the document (xmlns:foo="http://www.foo.org/") is available in your expression with the same prefix, wherever it is declared. If the document declares a default namespace (xmlns="…"), unprefixed element names in the expression are resolved in that namespace as well, so //cd still works. The prefixes fn: (standard functions), math:, map:, array:, xs: (schema types) and xml: are reserved and always available; fn: is the default function namespace, so you may write count() instead of fn:count(). To match elements regardless of their namespace use *:cd (XPath 3) or //*[local-name()='cd'].

Item types

When "Include 'XML Item Type'" is ticked, each result shows what kind of item it is: Element, Attribute, Text, Comment, ProcessingInstruction or Document for nodes; xs:string, xs:integer, xs:decimal, xs:double, xs:boolean, xs:date… for atomic values; array and map for XPath 3.1 structures. Element results are pretty-printed; attributes are shown as name="value". "Show result as text only" prints the string value of every item instead (the text content of elements), which is convenient for copying data out of a document.