Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nokogiri 'not' selector

Tags:

ruby

nokogiri

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'])")
like image 322
Jamie Avatar asked Mar 09 '11 17:03

Jamie


3 Answers

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

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

(untested, but the selector is right)

like image 124
tvon Avatar answered Sep 22 '22 16:09

tvon


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

.local:not(.hide) 
like image 43
Victor Ivanov Avatar answered Sep 25 '22 16:09

Victor Ivanov


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

dom.xpath("//wherever/*[not (@class='someclass')]")
like image 38
drewrobb Avatar answered Sep 25 '22 16:09

drewrobb