Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath test for ancestor attribute not equal string

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!

like image 487
Jeff Avatar asked Jun 21 '12 18:06

Jeff


1 Answers

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
like image 88
O. R. Mapper Avatar answered Oct 15 '22 13:10

O. R. Mapper