You have not registered an XML namespace before the request.
// register it $xpath->registerNamespace('kml', "http://earth.google.com/kml/2.1"); // and use it, too ! $query = $xpath->query("//kml:Placemark[kml:type='Public/Daily Fee']");
( ancestor::kml:Placemark in your expression does not make sense, I left it.)
The namespace prefix in the XPath query should be used even if the namespace in question is the default namespace of the XML document (for example, in your case).
The element <kml xmlns="http://earth.google.com/kml/2.1"> equivalent to <kml:kml xmlns:kml="http://earth.google.com/kml/2.1" >.
However, the XPath /kml query is not equivalent to /kml:kml . The latter will correspond to both elements (when the kml namespace is registered in advance), the former will not match any of them, no matter what you do.
Also note that you do not need to use the same namespace prefix that is used in the XML document.
Assume an XML document:
<kml:kml xmlns:kml="http://earth.google.com/kml/2.1"> <kml:Document /> </kml:kml>
Then you can still do this:
$xpath->registerNamespace('foo', "http://earth.google.com/kml/2.1"); $query = $xpath->query("/foo:kml/foo:Document");
and return the correct result. Namespace prefixes are simply shorthand.
Tomalak
source share