Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

XPath axe

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.

descendant:: selects all nodes below the context at any depth; descendant-or-self:: also includes the context itself. The familiar // is an abbreviation for /descendant-or-self::node()/, which is why it can appear mid-path as well as at the start.

It is the most convenient axis and the most expensive: each // walks the whole subtree. Where you know the structure, an explicit path is faster and more precise.

Syntax
descendant::name — // is descendant-or-self::node()/
Returns
node-set
Kind
Axe
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.

  • count(//line)on order.xmlRun
    3
  • count(/order/descendant::line)on order.xmlRun
    3
  • count(//price)on order.xmlRun
    3

    Three levels down, reached without naming the levels.

  • count(descendant::line)on order.xmlRun
    3

    Relative to the document root, so the same result here.

Run your own expression

What catches people out

  • // at the start of an expression is absolute — it means 'anywhere in the document', not 'anywhere below here'. Use .// for the relative form.
  • //a//b walks the subtree twice. On large documents that is the difference between instant and not.
  • descendant:: does not include attributes.

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.

order.xml
<?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

All 34 XPath entries

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.