tags:

views:

1503

answers:

2

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

+4  A: 

looks like you probably landed on a bug there. It looks 100% fine to me.

as to your related question, use evaluate 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? 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)

Jonathan Fingland
Thx, but using evaluate() didn't help. I tried adding the namespace, too - no success. I don't know for sure, but I think the namespaces are extracted out of the document by PHP, anyway.
soulmerge
in that case, go with the abbreviated form. I tend to prefer the verbose format in many cases (descendant-or-self for example) but I can't say I've tried to use the verbose attribute:: axis
Jonathan Fingland
Many thanks for actually testing it :)
soulmerge
A: 

Does //*@url work?

Ollie Saunders