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!';
}
精彩评论