首页 > 解决方案 > 选择给定节点下一个类型的所有节点

问题描述

我是 XNode 的新手,想在给定节点下选择一种类型的所有节点。在以下示例中,我正在查找 foo 节点下的所有 bar 节点。

 <node>
    <foo>
     <bar/>
     <div><bar/></div>
     <ul>
      <li><bar/>
     </ul>
     <p>foobar</p>
    </foo>
    <bar/>
</node>

我的应用程序获取 foo 节点(org.w3c.dom.Node):

NodeList nodeList = (NodeList) xpath.evaluate("//bar", fooNode, XPathConstants.NODESET);

返回整个文档的所有 bar 节点,而不是来自 fooNode,即使我传递了节点而不是整个文档。

标签: javaxpath

解决方案


返回整个文档的所有 bar 节点,而不是来自 fooNode,即使我传递了节点而不是整个文档。

这是绝对位置路径的预期行为,如//bar. 使用相对位置路径作为.//bardescendant-or-self::bardescendant::bar


推荐阅读