开发者

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

开发者 https://www.devze.com 2023-01-24 06:38 出处:网络
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

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 -->
       开发者_StackOverflow <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>


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.


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

live link

0

精彩评论

暂无评论...
验证码 换一张
取 消