Xpath test for ancestor attribute not equal to string

I am trying to check if an attribute for an ancestor of an element is not equal to a string.

Here is my XML ...

<aaa att="xyz"> <bbb> <ccc/> </bbb> </aaa> <aaa att="mno"> <bbb> <ccc/> </bbb> </aaa> 

If I work on the ccc element, I try to verify that his grandparents aaa @att are not equal to "xyz".

I currently have this ...

 ancestor::aaa[not(contains(@att, 'xyz'))] 

Thanks!

+6
source share
1 answer

Assuming that by specifying the ancestor of the element, you are referencing the element with children, this XPath expression should do:

 //*[*/ccc][@att != 'xyz'] 

He is choosing

  • all nodes
  • who have at least one <ccc> grandson node
  • and have the attribute att whose value is not xyz .

Update: The limited test for grandparents <ccc> .

Update 2: Adapted to your revised question:

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

The choice

  • all <ccc> elements
  • who have grandparent <aaa> with their att attribute set to a value that is not xyz
+15
source

Source: https://habr.com/ru/post/918690/


All Articles