cbmanica cbmanica - 1 year ago 110
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.

Answer Source

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