Home > Blockchain >  Find top-most following elements with XPath
Find top-most following elements with XPath

Time:12-23

In XPath, I know I can select all following elements with /following::*, however I'd like to avoid also selecting the children contained within any following elements.

For example, given this document:

<body>
    <div id="div1">
        <p id="p1">...</p>
        <p id="p2">
            <span id="span1"></span>
            <span id="span2"><i id="i1">...</i></span>
        </p>
        <p id="p3">...</p>
    </div>
    <div id="div2">
        <p id="p4">...</p>
        <p id="p5">...</p>
    </div>
</body>

If I have span1 selected, I would like to select span2 (but not i1), p3, and div2 (but not p4 or p5).

In Python, my code might look something like:

>>> lxml.html.fromstring(document).xpath('//*[@id="span1"]/following::*')
[<Element span at 0x1082bd680>,
 <Element i at 0x1082bd4f0>,
 <Element p at 0x1082bd770>,
 <Element div at 0x1082bd360>,
 <Element p at 0x1082bd7c0>,
 <Element p at 0x1082bdef0>]

But what I'd like to have returned is:

[<Element span at 0x1082bd680>,
 <Element p at 0x1082bd770>,
 <Element div at 0x1082bd360>]

EDIT: @kjhughes answer got me 90% of the way there. Because the real life example might not have a ID that I can easily use to match, I ended up writing code like:

find_following = lxml.html.etree.XPath(
    "following::*[not(../preceding::*[. = node()])]"
)

CodePudding user response:

This XPath,

//*[@id="span1"]/following::*[not(../preceding::*[@id="span1"])]

selects the elements following the targeted span element whose parents do not have the targeted span element as a predecessor,

<span id="span2"><i id="i1">...</i></span>
<p id="p3">...</p>
<div id="div2"> <p id="p4">...</p> <p id="p5">...</p> </div>

as requested.

CodePudding user response:

XPath 3.1 has the function outermost(): outermost(following::*) selects all following elements excluding any that are descendants of another element in the node-set.

XPath 2.0 allows following::* except following::*/descendant::*.

In XPath 1.0 you can express ($A except $B) as $A[count(.|$B)=count($B)]. (Though this isn't all that useful because there's no way within XPath itself of binding a variable).

  • Related