XPath does not match attributes without a namespace as a prefix

I am currently trying to read various install.rdf files of Firefox extensions through PHP SimpleXML.

Unfortunately, there is no predefined structure of how they should look. They are always using two namespaces, " http://www.w3.org/1999/02/22-rdf-syntax-ns# " and " http://www.mozilla.org/2004/em-rdf# "

So my idea was to use XPath to get the elements of interest:

$xml = simplexml_load_string($installRDF); $namespaces = $xml->getNameSpaces(true); $xml->registerXPathNamespace('rdf', NS_RDF); $main = $xml->xpath('/rdf:RDF/rdf:Description[@rdf:about="urn:mozilla:install-manifest"]'); 

But there seems to be a problem with the rdf prefix of the about attribute, as it simply returns the result if there is also a prefix defined in the RDF file.

So for this it works:

 <RDF:RDF xmlns:em="http://www.mozilla.org/2004/em-rdf#" xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <RDF:Description RDF:about="urn:mozilla:install-manifest"> <em:id> extension@mozilla.org </em:id> </RDF:Description> </RDF:RDF> 

But for this not:

 <RDF xmlns:em="http://www.mozilla.org/2004/em-rdf#" xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <Description about="urn:mozilla:install-manifest"> <em:id> extension@mozilla.org </em:id> </Description> </RDF> 

This looks like a bug for PHP, because if I remove an attribute from XPath, I always get Description elements. But I still don't know about using namespaces in XPath, so I ask here.

+4
source share
1 answer

The problem is that the attributes of your second example are in an empty namespace. The problem is not the query, but the XML data from your two examples is not equivalent.

See Namespaces in XML 1.0 (Third Edition) :

The default namespace declaration applies to all elements that do not have prefix names within its scope. Namespace declarations do not apply by default. directly for name attributes; The interpretation of unsigned attributes is determined by the element on which they are displayed.

+1
source

All Articles