Is it possible to select an h1 tag that does not contain any img tags with a single line XPath expression? If so, what is it?
This works if the img tag is a direct child of <h1>. Is it possible to check if there is no img tag at any level within the h1 tag?
Pheter
2010-01-02 23:23:48
This appears to be the cleanest solution, but it will not check if an img tag exists that is a child of a child of an h1 tag. Is it possible to check recursively through each child of the h1 tag and their children to ensure that there is no img tag?
Pheter
2010-01-02 23:25:39
Well, the `not()` argument is also just a XPath, you can use anything there. Updated the answer.
Lukáš Lalinský
2010-01-02 23:27:45
Thanks, your updated solution is just what I was looking for but Andrew Arnott beat you to it :P
Pheter
2010-01-02 23:30:23
+6
A:
Use the not
operator and the descendent
axis to catch h1 tags without even a distant img child.
//h1[not(descendant::img)]
Andrew Arnott
2010-01-02 23:26:38
A:
To find an h1
element with no img
child:
//h1[not(img)]
To find an h1
element with no img
descendant:
//h1[not(.//img)]
or
//h1[not(descendant::img)]
which might be easier to understand when reading your code.
Robert Rossney
2010-01-02 23:28:16