我是 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 返回,即使我传递的是节点而不是整个文档。
最佳答案
Returns all bar nodes of the whole document, not from the fooNode even though i passed the node and not the whole document.
这是 absolute location path 的预期行为作为//bar
。使用相对位置路径作为 .//bar
或 descendant-or-self::bar
或 descendant::bar
关于java - 选择给定节点下某种类型的所有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720722/