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 -->
开发者_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
精彩评论