Cesar
Cesar

Reputation: 4116

php xpath select div with class "A" only if it has a child with class "a1"

Given the following html fragment, how can i select all the div's with class a or c only if they have a child div with class a1

Rigth now i have the following xpath query:

//div[@id='z']/div[@class='a' or @class='c']

<div id="z">
    <div class="a"><!-- Should be  selected -->
        <div class="a1"></div>
    </div>
    <div class="a">
        <div></div>
    </div>
    <div class="b">
        <div></div>
    </div>
    <div class="a"><!-- Should be  selected -->
        <div class="a1"></div>
    </div>
    <div class="c"><!-- Should be  selected -->
        <div class="a1"></div>
    </div>
</div>

Upvotes: 2

Views: 7294

Answers (2)

Galen
Galen

Reputation: 30170

/div[@id='z']/div[@class='a' or @class='c'][div[@class='a1']]

live link

Upvotes: 3

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

Use:

//div[(@class='a' or @class='c') and div[@class='a1']]

Always avoid using // when the structure of the XML is known. // is often very inefficient, because it causes the traversal of the complete subtree whose top element is the context node.

Upvotes: 4

Related Questions