PHP and Xpath - Get node from inner text



I have the following XML structure



<url>
<loc>some-text</loc>
</url>

<url>
<loc>some-other-text</loc>
</url>


My goal is to get loc node from it's inner text (i.e. some-text) or a part of it (i.e. other-text). Here's my best attempt:



$doc = new DOMDocument('1.0','UTF-8');
$doc->load($filename);
$xpath = new Domxpath($doc);
$locs = $xpath->query('/url/loc');

foreach($locs as $loc) {
if(preg_match("/some-text/i", $loc->nodeValue)) return $loc->parentNode;
}


Is it possible to get specific loc node without iterating over all nodes, simply using xpath query?


1 comment: