xml - 选择具有祖先条件的节点
问题描述
我的问题:我需要检查“testElement”的属性“attributeToBeTested”。“testElement”是“test”的唯一孩子。一个条件:这个“test”元素必须是“toto”的子元素,而不是“tata”或“titi”的子元素。
所以我把我的测试写成:
//toto/descendant::test/descendant::testElement[attribute::attributeToBeTested != "Z"] and //test[not(ancestor::tata)] and //test[not(ancestor::titi)]
我想我写的是:给我所有的“test”元素,“toto”的子元素,其中“testElement”元素的“attributeToBeTested”未设置为“Z”并且没有“tata”或“titi” “作为祖先。
但显然,这个测试是错误的,这让我发疯!
这是 XML 示例:
<tata>
....
<test>
<testElement attributeTobeTested="Z"/>
</test>
</tata>
<toto>
....
<test>
<testElement attributeTobeTested="H"/>
</test>
....
<test>
<testElement attributeTobeTested="Z"/> <---- This one should pop up
</test>
</toto>
<toto>
....
<titi>
<test>
<testElement attributeTobeTested="Z"/>
</test>
</titi>
</toto>
只有一个应该弹出:“toto”内的那个不在“tata”中,也不在“titi”中,并且属性为“Z”
解决方案
试试下面的 XPath:
//toto//test[not(ancestor::*[name()=("titi", "tata")])]/testElement[@attributeTobeTested="Z"]
它应该返回您所需testElement
的节点
推荐阅读
- node.js - 使用 pg npm 从 nodejs 通过 teiid 连接到 vdb 时连接意外终止
- python - Python:将两个列表与嵌套的while循环结合起来
- vue.js - 迭代中的元素期望有 'v-bind:key' 指令。eslint-plugin-vue
- ios - iOS 我可以同时维护同一个应用的两个应用版本吗?
- vue.js - 如何测试 bootstrap-vue 吐司
- angular - 谷歌图表水平线显示在栏的前面,当我悬停图例时显示小线。请帮我
- reporting-services - 是否可以在 SSRS 的单个行分组中使用 2 个不同的数据集?
- python - pylint 无效名称错误 (C0103) 不遵循 python 约定
- docker - jwilder/nginx-proxy 和 cloudflare 在使用 https 请求时出现错误 500
- python - 尝试通过 Python 中的 Shopify API 更新页面