问题
I'm trying to test if an attribute on an ancestor of an element not equal a string.
Here is my XML...
<aaa att="xyz">
<bbb>
<ccc/>
</bbb>
</aaa>
<aaa att="mno">
<bbb>
<ccc/>
</bbb>
</aaa>
If I'm acting on element ccc, I'm trying to test that its grandparent aaa @att doesn't equal "xyz".
I currently have this...
ancestor::aaa[not(contains(@att, 'xyz'))]
Thanks!
回答1:
Assuming that by saying an ancestor of an element you're referring to an element with child elements, this XPath expression should do:
//*[*/ccc][@att != 'xyz']
It selects
- all nodes
- that have at least one
<ccc>
grandchild node - and that have an
att
attribute whose value is notxyz
.
Update: Restricted test to grandparents of <ccc>
.
Update 2: Adapted to your revised question:
//ccc[../parent::aaa/@att != 'xyz']
Selects
- all
<ccc>
elements - that have a grandparent
<aaa>
with its attributeatt
set to a value that is notxyz
来源:https://stackoverflow.com/questions/11144805/xpath-test-for-ancestor-attribute-not-equal-string