What would be the right validation if i want to know that my XPath Query is not working..
$query_id = $xpath->query('//tr[@class="calendar_row"][@data-eventid]/@data-evenid');
if (!is_null($query_id)) {
...
} else {
echo 'Invalid Query!';
}
I'm using the code above but it displays nothing even if the query cannot produce any result.
So the way I understand this is you want to check for an empty list. Here is what PHP.net says about DOMXPath::query():
Returns a DOMNodeList containing all nodes matching the given XPath expression. Any expression which does not return nodes will return an empty DOMNodeList.
If the expression is malformed or the contextnode is invalid, DOMXPath::query() returns FALSE.
With that said, you must check for an empty DOMNodeList, which may not actually return FALSE when checked with a negation since it's an "Traversable" object.
Instead just use something like the following:
$query_id_entries = $xpath->query('//tr[@class="calendar_row"][@data-eventid]/@data-evenid');
if ($query_id_entries->length == 0) {
echo "invalid query";
} else {
foreach($query_id_entries as $query_id) {
// ...
}
// Or you could do the following.
// $query_id = $query_id_entries->item(0);
}