XPath operator
Path steps / and //
A single slash is one level; a double slash is any depth. A leading slash makes the whole path absolute.
/ separates steps and, at the start of an expression, anchors the path to the document root. // is an abbreviation for /descendant-or-self::node()/, so it matches at any depth and can appear anywhere in a path.
The distinction that catches people is relative versus absolute. Inside a predicate, //x still starts at the document root — the relative form is .//x.
- Syntax
/step/step — //step- Returns
- node-set
- Kind
- Operator
- Version
- XPath 1.0 (libxml2)
Worked examples
Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.
The sample documents
Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
<customer country="NO">Ada Lovelace</customer>
<lines>
<line sku="WIDGET-1" qty="2"><price>14.50</price></line>
<line sku="WIDGET-2" qty="1"><price>39.00</price></line>
<line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
</lines>
<note> Deliver after 09:00 </note>
</order>Related
- child:: axisImmediate children. The default axis, which is why it is almost never written out.
- descendant:: and descendant-or-self::Everything below a node, at any depth. // is shorthand for descendant-or-self, which is why //a//b can be expensive.
- self:: axisThe context node itself. Abbreviated as a dot, and used to test what the current node is.
- Predicates [ ]Filters a node-set. Order matters, and a numeric predicate is not the same as a positional one applied afterwards.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.