Jamie
Jamie

Reputation: 2275

Nokogiri 'not' selector

Is there a way in Nokogiri to select all elements that don't match a selector. In jQuery I'd use:

:not(*[@class='someclass'])

However the following code gives me an xpath syntax error

dom = Nokogiri::HTML(@file)
dom.css(":not(*[@class='someclass'])")

Upvotes: 6

Views: 6396

Answers (3)

Victor Ivanov
Victor Ivanov

Reputation: 130

In addition to ton's answer, if you want to use two classes, that it would like this:

.local:not(.hide) 

Upvotes: 4

tvon
tvon

Reputation: 1513

In CSS3, :not() takes a selector like any other, so it would be:

dom.css(":not(.someclass)")

(untested, but the selector is right)

Upvotes: 12

drewrobb
drewrobb

Reputation: 1604

I'm not sure about the syntax you are using, but this is basically xpath selector you want:

dom.xpath("//wherever/*[not (@class='someclass')]")

Upvotes: 3

Related Questions