如果元素具有 xmlns 属性,Xpath 将失败

发布于 2024-10-21 12:18:00 字数 1579 浏览 9 评论 0原文

可能的重复:
xPath 只找到*

我正在尝试使用 xml 来解析 .COLLADA 文件。问题是,如果根标记具有 xmlns 属性,我似乎无法使用 xpath() 访问元素。

例如,这可以工作:

$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']"));

但这不行:

$string = <<<TEST
<?xml version="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']"));

xmlns 是如何突然使 xml 树不可用的?我认为它只是定义了名称空间,这样您就可以将它与其他名称空间中的其他相同标签区分开来。我缺少什么?

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 version="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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-10-28 12:18:00

您需要注册名称空间:

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

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']"));
舟遥客 2024-10-28 12:18:00

命名空间声明意味着名称位于命名空间中,但您的 XPath 正在寻找非命名空间中的名称。搜索命名空间元素的方式取决于您使用的是 XPath 1.0 还是 2.0,并且在 XPath 中使用命名空间前缀来表示命名空间名称的情况下,声明前缀的方式取决于 XPath 处理器的 API你正在使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文