I'm just trying to exclude a couple of child elements by class from a selection of all the children of a node
page.css('div.parent > *').each do |child|
if (child["class"].content != 'this' && child["class"].content != 'that')
array.push(child.text.to_s)
end
end
I know this not the write syntax, but have been unable to find how to select an elements class, as opposed to selects and element by class.
The
css
method gives youNokogiri::XML::Element
instances and those get most of their behavior from theirNokogiri::XML::Node
parent class. To get an attribute out of a Node, use[]
:You could also use
if(child['class'] != 'this' && child['class'] != 'that')
if that makes more sense to you.However,
class
attributes can have multiple values so you might want to split them into pieces on whitespace:The intersection is just an easy way to see if the two arrays have any elements in common (i.e.
classes
contains anything that you want to exclude).