I'm trying to select all dom elements that have id="mydiv"
but exclude the ones that also have the class="exclass"
. Right now I'm doing the first part //*[@id="mydiv"]
. How do I add the class exclusion part?
P.S. In case you're wondering why I need to select multiple elements that have the same id, I'm just working on an existing DOM that I can't control.
You can use negation:
//*[@id="mydiv" and @class!="exclass"]
If the class
attribute may not exist on all nodes, you need this:
//*[@id="mydiv" and (not(@class) or @class!="exclass")]
The last (somewhat) odd logic can be turned into what Michael proposed:
//*[@id="mydiv" and not(@class="exclass")]
Though, personally, the fact that XPath cannot make comparisons if the attribute is missing feels a bit like a shortcoming.
The answer to the question as written is
//*[@id="mydiv" and not(@class="exclass")]
The first half of the condition is true if there is an @id attribute and its value is mydiv. The second half is true if there is no @class attribute with the value exclass: that is, if there is no class attribute, or if there is a class attribute and its value is something other than "exclass".
Avoid using != in this situation: 90% of the time, if you think of writing A!=B
, you probably wanted not(A=B)
. The meaning is different in the case where either A or B is not a singleton, that is, where it is either empty, or can contain multiple values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With