From: 7stud -- Date: 2012-12-16T19:18:42+09:00 Subject: Re: print - and strip text between tags using Nokogiri Robert Klemme wrote in post #1089225: > > puts dom.xpath('/html/body//pre/text()').map(&:to_s) > Calling map() is redundant because puts calls to_s on its arguments. > > 2) to print all of the non-tagged text between the > "body" comments > Your html doesn't even test your requirements because there is no text after the body="end' comment. And there is no non-tagged text: require 'nokogiri' html = < My Title

My Heading

From:Me
Date: Wed Dec 05 2012 - 18:17:49 EST

text line 1
text line 2
text line 3

very important text
more important text
would you believe even more important text?

text line 4
text line 5 HTML doc = Nokogiri.HTML(html) my_xpath = "/html/body/comment()[1]/following-sibling::*" doc.xpath(my_xpath).each do |node| puts node.name puts node.text puts '*' * 20 end --output:-- p text line 1 text line 2 text line 3 ******************** p ******************** pre very important text more important text would you believe even more important text? ******************** p ******************** p text line 4 text line 5 ******************** --output:-- text line 1 text line 2 text line 3 doc = Nokogiri.HTML(html) my_xpath = "/html/body/comment()[1]/following-sibling::*[not(self::pre)]" catch :found_ending_text do doc.xpath(my_xpath).each do |node| node.children.each do |child| text = child.text throw :found_ending_text if text.include? %q{body} next if text.empty? puts text.strip end end end --output:-- text line 1 text line 2 text line 3 -- Posted via http://www.ruby-forum.com/.