4

Currently I'm trying to read different install.rdf files of Firefox extensions via PHP's SimpleXML.

Unfortunately there's no predefined structure how they have to 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 an 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 regarding the rdf prefix of the about attribute, because it just returns a result, if there's 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 in PHP to me, since if I remove the attribute from the XPath I am always getting the Description elements. But I am not aware of using namespaces in XPath yet, so I am asking here.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132

1 Answers1

1

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

See Namespaces in XML 1.0 (Third Edition):

A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • Ok, default namespaces do not apply **directly** to attribute names. That reads to me like unprefixed attributes get the same namespace as the element they are in, which would be fine. Though checking the namespace of them I can see it is _null_, so indeed the two XML examples are not the same. Thanks for the hint! – Sebastian Zartner Aug 03 '11 at 06:01