XPath: limit scope of result set

Posted by Laramie on Stack Overflow See other posts from Stack Overflow or by Laramie
Published on 2011-01-10T20:46:01Z Indexed on 2011/01/10 21:54 UTC
Read the original article Hit count: 200

Filed under:

Given the XML

<a>
    <c>
        <b id="1" value="noob"/>
    </c>
    <b id="2" value="tube"/>
    <a>
        <c>
            <b id="3" value="foo"/>
        </c>
        <b id="4" value="goo"/>
        <b id="5" value="noob"/>
        <a>
            <b id="6" value="near"/>
            <b id="7" value="bar"/>
        </a>
    </a>
</a>

and the Xpath 1.0 query

//b[@id=2]/ancestor::a[1]//b[@value="noob"]

is there some way to limit the result set to the <b> elements that are ONLY the children of the immediate <a> element of the start node (//b[@id=2])?

For example the Xpath above returns both node ids 1 and 5. The goal is to limit the result to just node id=1 since it is the only @value="noob" element in the same <c> group as our start node (//b[@id=2]).

In English, "Starting at a node whose id is equal to 2, find all the elements whose value is "noob" that are descendants of the immediate parent c element without passing through another c element".

© Stack Overflow or respective owner

Related posts about xpath