开发者

Xpath fails if an element has a a xmlns attribute [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

xPath finds nothing but *

Im trying to use xml to parse a .COLLADA file. The problem is I can't seem to use xpath() to access elements if the root tag has a xmlns attribute.

For example this works:

$string = <<<TEST
<?xml version="1.0" encoding="utf-8"?>
<COLLADA version="1.4.1">
  <library_materials>
    <material id="Material" name="Material">
      <instance_effect url="#Material-effect"/>
    </material>
    <material id="Material2" name="Material">
      <instance_effect url="#Material-effect2"/>
    </material>
  </library_materials>
</COLLADA>
TEST;
$lol = new SimpleXMLElement($string);
print_r($lol->library_materials->xpath("material[@id='Material2']"));

But this doesn't:

$string = <<<TEST
<?xml 开发者_如何学Pythonversion="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">
  <library_materials>
    <material id="Material" name="Material">
      <instance_effect url="#Material-effect"/>
    </material>
    <material id="Material2" name="Material">
      <instance_effect url="#Material-effect2"/>
    </material>
  </library_materials>
</COLLADA>
TEST;
$lol = new SimpleXMLElement($string);
print_r($lol->library_materials->xpath("material[@id='Material2']"));

How does the xmlns suddenly make the xml tree unusable? I thought it just defined the namespace so you could tell it apart from other identical tags in other namespaces. What am I missing?


You need to register the namespace:

$lol = new SimpleXMLElement($string);
$lol->registerXPathNamespace('cada', 'http://www.collada.org/2005/11/COLLADASchema');
print_r($lol->xpath("child::cada:library_materials//*[@id='Material2']"));


The namespace declaration means that the names are in a namespace, but your XPath is looking for names in no namespace. The way you search for namespaced elements depends on whether you are using XPath 1.0 or 2.0, and to the extent that you use namespace prefixes in your XPath to represent namespaced names, the way that you declare the prefixes depends on the API to the XPath processor you are using.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜