属性不相等或不存在的元素的 XPath [英] XPath for elements with attribute not equal or does not exist
问题描述
我有以下 XML 格式:
I have the following XML format:
<test>
<table>
<rows>
<row ID = "1" Name ="A"/>
<row ID = "2" SubID = "1" Name ="B"/>
<row ID = "3" Name ="C"/>
<row ID = "4" SubID = "1" Name ="D"/>
<row ID = "5" Name ="E"/>
<row ID = "4" SubID = "2" Name ="E"/>
</rows>
.
.
</table>
</test>
我想获取所有行,除了那些具有 SubID = 1
的行.
I would like to take all the rows except the ones that do have SubID = 1
.
难点在于,并非所有行都具有名为 SubID
的属性,并且并非所有具有 SubID
属性的行都具有相同的属性价值.
The hard part of this, is that not all the rows have attribute called SubID
, and that not all the rows with the attribute SubID
, have same value.
理想的输出应该是:
<row ID = "1" Name ="A"/>
<row ID = "3" Name ="C"/>
<row ID = "5" Name ="E"/>
<row ID = "4" SubID = "2" Name ="E"/>
我尝试使用否定值的 XPath,但这不起作用:
I have tried to use an XPath with negation on value, but this won't work:
/test/table/row/not(@SubID=1)
有什么帮助吗?
推荐答案
困难的部分"其实很简单: 一个基本的
element[not(@
attribute='
value')]
谓词将排除元素没有属性和元素,其中attribute 等于 value.
predicate will exclude elements without the attribute and elements for which attribute equals value.
因此,在您的特定情况下,所有 @SubID
不等于 1
的 row
元素:
So, in your specific case, all row
elements that do not have @SubID
equal to 1
:
/test/table/rows/row[not(@SubID = '1')]
选择
<row ID="1" Name="A"/>
<row ID="3" Name="C"/>
<row ID="5" Name="E"/>
<row ID="4" Name="E" SubID="2"/>
根据要求.
这篇关于属性不相等或不存在的元素的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!