开发者

Evaluate if result of DOMXpath->query returns match

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)?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜