Skip to main content
XMLDir

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

XPath axe

following-sibling:: and preceding-sibling::

Siblings after (or before) the context node. preceding-sibling:: is a reverse axis, which changes what [1] means.

These two axes walk sideways rather than up or down, selecting nodes that share a parent with the context node. following-sibling:: takes those after it in document order; preceding-sibling:: those before.

preceding-sibling:: is a reverse axis, so preceding-sibling::*[1] is the immediately previous sibling — the nearest, not the first in the document.

Syntax
following-sibling::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(//line[1]/following-sibling::line)on order.xmlRun
    2
  • //line[1]/following-sibling::line[1]/@skuon order.xmlRun
     sku="WIDGET-2"
  • //line[3]/preceding-sibling::line[1]/@skuon order.xmlRun
     sku="WIDGET-2"

    [1] on the reverse axis gives the immediately previous sibling.

Run your own expression

What catches people out

  • preceding-sibling::*[1] is the nearest previous sibling, not the document's first.
  • These axes stay within one parent. following:: and preceding:: cross the whole document and are almost never what you want.
  • Whitespace text nodes are siblings too, so following-sibling::node()[1] is often a newline.

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.