java - 选择给定节点下一个类型的所有节点
问题描述
我是 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,即使我传递了节点而不是整个文档。
解决方案
返回整个文档的所有 bar 节点,而不是来自 fooNode,即使我传递了节点而不是整个文档。
这是绝对位置路径的预期行为,如//bar
. 使用相对位置路径作为.//bar
或descendant-or-self::bar
或descendant::bar
推荐阅读
- xcode - 带有 iOS 模拟器 iPhone X(12.0) 的 Mac OS Big Sur 无法使用错误 _ReportRPCTimeout
- xpath - 带有特定后代标签的标签的 XPath 选择器选择其他标签
- xcode - 为什么 Xcode 缓存如此庞大?
- c - 按值传递链表给函数
- c# - 什么时候创建闭包?
- xamarin.android - 缺少 Xamarin.Android.Things 的共享库
- java - 使用声明式 HTTP 客户端 Junit 5 进行 Micronaut 测试
- azure - 如何使用 CosmosDB SQL API 加入另一个数组中的数组?
- typescript - Webdriver.io / wdio.conf.ts:对象字面量只能指定已知属性,而“配置”类型中不存在“mochaOpts”
- command - Mplayer - 使用命令行更改曲目、播放/暂停