Reputation: 147
I have this kind of HTML file:
<div class="find-this">I do not need this</div>
<div class="content ">
<div class="find-this">
<span class="yellowcard"></span>
<span class="name">Cristiano Ronaldo</span>
</div>
</div>
<div class=" content">
<div class="find-this">
<span class="redcard"></span>
<span class="name">Lionel Messi</span>
</div>
</div>
So far, I get the find-this
class that are in content
parent class.
$nodes = $xpath->query("//div[contains(@class,'content')]//div[@class='find-this']");
foreach ($nodes as $key => $node) {
echo "Player ". $key .": " . $node->nodeValue;
}
Result:
Player0: Cristiano Ronaldo
Player1: Lionel Messi
How I can find out which find-this
class is parent of <span class="yellowcard">
and which one is parent of <span class="redcard">
?
Thank you in advice.
Upvotes: 1
Views: 894
Reputation: 11961
To select the find-this
div
which is a parent of <span class="yellowcard">
and the div
which is parent of <span class="redcard">
use the XPaths shown below:
$yellow_nodes = $xpath->query("//span[@class='yellowcard']/parent::div[@class='find-this']");
$red_nodes = $xpath->query("//span[@class='redcard']/parent::div[@class='find-this']");
Upvotes: 1