I want to use xpath query to retrieve the "Testing" only once, in the following test.html
<html>
<body>
<div class="test1"></div>
<div class="test2">
<div><strong>Testing</strong></div>
</div>
</body>
</html>
Here is the php code I used to retrieve the content.
$uri='test.html';
$doc = new DOMDocument('1.0','utf-8');
$doc->loadHTMLFile($uri);
$xpath= new DOMXPath($doc);
$path="/html/body/div[2]//*";
$elements = $xpath->query($path);
if(!is_null($elements)){
foreach($elements as $element){
echo '<br>['.$element->nodeName.']';
$nodes = $element->childNodes;
foreach($nodes as $node){
$nodeValue=$node->nodeValue;
echo $nodeValue;
}
}
}
Here is the result I got.
[div] Testing
[strong] Testing
Why does it print "Testing" even in the [div] node? I want it only retrieve "Testing" when it is in [strong] node.