XPath axe
preceding:: axis
Every node before the context node's opening tag, excluding its ancestors. A reverse axis, so [1] means nearest.
preceding:: selects nodes that end before the context node begins. It crosses parent boundaries but excludes ancestors, which contain the context rather than preceding it.
It is a reverse axis. A positional predicate attached directly to it counts from the context backwards; parentheses turn the result into document order before a later predicate runs.
- Syntax
preceding::name- 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(/order/note/preceding::line)on order.xmlRun3/order/note/preceding::line[1]/@skuon order.xmlRunsku="GIFT-WRAP"[1] on the reverse axis selects the nearest preceding line.
(/order/note/preceding::line)[1]/@skuon order.xmlRunsku="WIDGET-1"Parentheses materialize the set in document order, so [1] now means the earliest line.
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>
<!-- priority customer -->
<?audit retained?>
<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
- following:: axisEvery node after the context node's closing tag, across the rest of the document rather than only among its siblings.
- following-sibling:: and preceding-sibling::Siblings after (or before) the context node. preceding-sibling:: is a reverse axis, which changes what [1] means.
- ancestor:: and ancestor-or-self::Every node above the context, up to the root. Returned in reverse document order.
- 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.