Reputation: 75704
I'm new to xpath and am running into troubles with PHP's ancient XPath implementation. Is it correct, that //@url
is just the abbreviated form of //attribute::url
? I'm trying to query the flickr rss for testing, but the abbreviated query finds the correct nodes, whereas the verbose one returns 0 elements:
$xml = file_get_contents('http://api.flickr.com/services/feeds/geo/?format=rss_200');
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$xpath->query('//media:content/@url')->length > 0;
$xpath->query('//media:content/attribute::url')->length == 0;
Did I misunderstand the w3c specs or is this a PHP bug?
Related question:
The successful query returns DOMAttr
objects as expected. But is there no way to get the text value of these nodes directly? Something like //media:content/attribute::url/child::text()
? I would need that for a tool where the xpath queries will be entered by users and the error detection would be much better if I could expect DOMText objects (instead of coding for a multitude of DOM* objects).
Upvotes: 1
Views: 4669
Reputation: 57167
looks like you probably landed on a bug there. It looks 100% fine to me.
as to your related question, use [evaluate][1] instead of query to get php to try to return a typed result. if it fails, it returns the normal nodelist
Edit:
Have you tried adding a [namespace][2]? the media: part might be throwing it for a loop. e.g.
$xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
Edit 2: just for reference, I also tried this in javascript using the following
function nsResolve() {
return "http://search.yahoo.com/mrss/"
}
document.evaluate("//media:content/attribute::url",document,nsResolve,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
and it works the same as using @url (currently 7 results) [1]: https://www.php.net/manual/en/domxpath.evaluate.php [2]: https://www.php.net/manual/en/domxpath.registernamespace.php
Upvotes: 4