cbmanica cbmanica - 9 months ago 65
Ruby Question

Nokogiri text node contents

Is there any clean way to get the contents of text nodes with Nokogiri? Right now I'm using

some_node.at_xpath( "//whatever" ).first.content

which seems really verbose for just getting text.


You want only the text?


Maybe you don't want all the whitespace and noise. If you want only the text nodes containing a word character,

doc.search('//text()').map(&:text).delete_if{|x| x !~ /\w/}

Edit: It appears you only wanted the text content of a single node:

some_node.at_xpath( "//whatever" ).text