开发者

XPath doesn't match attributes without namespace as prefix

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:Descript开发者_StackOverflow中文版ion>
</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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜