Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Select for Multiple Classes Using Nokogiri and Ruby

From a table element, I would like to select all rows that have the class even or the class odd.

I tried the jQuery syntax:

report.css("table.data tr[class~=odd even]").each{|line| parse_line_item(line)}

but it threw an error, any help is appreciated, thanks.

like image 366
Russ Bradberry Avatar asked Mar 27 '10 16:03

Russ Bradberry


1 Answers

Use two selectors: report.css("table.data tr.odd, table.data tr.even")

The ~= operator in a CSS attribute selector checks that the value matches a space-delimited list of classes. For instance, tr[class~=odd] would match <tr class="odd"> and <tr class="odd ball">. However, in the specific case of the class attribute, the better selector is simply tr.odd.

If you use the ~= operator with a space in the value (as in tr[class~="odd even"], the selector will never match anything.

like image 138
Ron DeVera Avatar answered Oct 12 '22 02:10

Ron DeVera