xml - XPath:选择特定父节点的子节点
问题描述
有几种情况
案例一:
<Root>
<Defaults Id="a"></Defaults>
</Root>
案例B:
<Root>
<Repeat>
<Defaults Id="b"></Defaults>
</Repeat>
</Root>
案例 C(嵌套“重复”的数量可能是无限的):
<Root>
<Repeat>
<Repeat>
<Repeat>
<Defaults Id="c"></Defaults>
</Repeat>
</Repeat>
</Repeat>
</Root>
案例 D:
<Root>
<Repeat>
<Page Id="p1">
<Defaults Id="d"></Defaults>
</Page>
</Repeat>
</Root>
我需要返回属于根元素或仅位于重复节点内部的默认节点的 XPath 查询。如果至少一个父节点不是重复节点或根节点,则不应将它们包含在结果中。因此查询结果应该返回测试用例 A、B、C 的节点。
谢谢!
解决方案
这个 XPath,
//Defaults[parent::Root or parent::Repeat]
将选择Default
父级为Root
or的所有元素Repeat
,
<Defaults Id="a"></Defaults>
<Defaults Id="b"></Defaults>
<Defaults Id="c"></Defaults>
按照要求。
推荐阅读
- android - 为什么DialogFragment的默认透明度没有覆盖屏幕的一部分?
- r - 在矩阵的每一列中随机将除一个 1 之外的所有内容更改为 0 的快速例程
- sql - 初学者 SQL 问题,试图搜索属于同一组的用户
- python - 类型错误:'在
' 需要字符串作为左操作数,而不是 dict_keys - c# - 将 RichTextBox 的 SelectionChanged 事件和组合框的 TextChanged 事件用于 FontSize 会引发 DependencyProperty.UnsetValue 错误
- postgresql - 使用 Sequelize 动态获取数据库 URL
- html - 响应式媒体查询 div 元素未将元素向左对齐
- javascript - 使用来自 https://maps.googleapis.com/maps/api/place/ 的图片
- html - 反应没有附加事件侦听器
- haproxy - 替换 HAproxy 2.x 的 regrep