XPath - all elements except those in header

Trying to figure out XPATH which match all elements except header or inside header. Let's assume that header can be detected by three conditions:

  1. outer tag is header eg. <header><div.....></header>
  2. outer tag has id which contains string "header"
  3. outer tag has class which contains string "header"

My xpath: //*[not(ancestor::header)] and //*[not(ancestor::*[contains(@id,"header")])] and //*[not(ancestor::*[contains(@class,"header")])]

is not correct.

EDIT: This should match all links which are inside header:

//*[ancestor::*[contains(@id,"header") or contains(@class,"header") or header]]

Now I want to get all elements except these.

Do you know how to make it work?

1 answer

  • answered 2018-03-11 14:01 Mads Hansen

    Each of the expressions in your original XPath were being evaluated separately, testing whether there is an element in the XML document that satisfies those conditions, and returning a boolean().

    Now that you have combined the predicates to order select the particular element(s) that you don't want, you just need to negate the test:

    //*[not(ancestor-or-self::header) and 
        not(ancestor::*[contains(@id,"header") or contains(@class,"header")])