Evaluate if result of DOMXpath->query returns match

1.9k Views Asked by At

How can I evaluate if DOMTXpath->query actually returns data. Right now I am doing ($xml is a DOMXpath object):

foreach($xml->query($xpath) as $node)
{
  echo $node->textContent;
}

But if I my $xpath doesn't result in a node it just doesn't output any data. I tried something like:

if ($xml->query($xpath))
{
echo "found";
}else{
echo "not found";
}

but that doesn't seem to work. How can I test if my query is actually returning a matching node (actually an attribute in this case)?

1

There are 1 best solutions below

0
On BEST ANSWER

Check the length parameter on the return of the query() function. xpath->query() returns a DOMNodeList.

$nodeList = $xpath->query();
if ($nodeList->length > 0) {
    echo 'Yay!';
} else {
    echo 'No!';
}