1

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 Answer 1

5

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!';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.