The preceding and following axes have the potential to select a large number of nodes, because they consider all nodes that come before (after) the context node in document order excluding ancestor nodes. The following axis excludes descendants, and the preceding axis excludes ancestors.

Also don’t forget: both axes exclude namespace nodes and attributes.

Input

<Records>
    <A id="1"/>
    <A id="2">
        <A id="2.1"/>
        <A id="2.2"/>
        <B id="2.3"/>
    </A>
    <B id="3"/>
</Records>

 

Examples

/Records/A[2]/A[2] is the context node, thus selections are performed relative to this element.

We have written example statements according to this assumption. Nonetheless, we will include it in XPath expressions to represent the full location.

 

> Select all preceding element nodes named A.

/Records/A[2]/A[2]/preceding::A

Result:

<A id="1"/>
<A id="2.1"/>

 

> The closest preceding element node named A.

/Records/A[2]/A[2]/preceding::A[1]

Result:

<A id="2.1"/>

 

> The furthest following element node named A.

/Records/A[2]/A[2]/following::A[last()]

Result:

No Match!

 

> The furthest following element node named B.

/Records/A[2]/A[2]/following::B[last()]

Result:

<B id="3"/>