cbmanica cbmanica - 1 year ago 128
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download