Xpath test for ancestor attribute not equal string

大城市里の小女人 提交于 2021-02-07 13:07:05

问题


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

  1. all nodes
  2. that have at least one <ccc> grandchild node
  3. and that have an att attribute whose value is not xyz.

Update: Restricted test to grandparents of <ccc>.

Update 2: Adapted to your revised question:

//ccc[../parent::aaa/@att != 'xyz']

Selects

  1. all <ccc> elements
  2. that have a grandparent <aaa> with its attribute att set to a value that is not xyz


来源:https://stackoverflow.com/questions/11144805/xpath-test-for-ancestor-attribute-not-equal-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!