views:

140

answers:

1
    doc = Nokogiri::HTML(open(url)).xpath("//*")
.xpath("//*[br]/text()[string-length(normalize-space()) != 0]")
.wrap("<span></span>")

    puts doc

it just returns the text ... i was expecting the full html source with now wrapped around the specified xpath elements.

+1  A: 

Try

doc = Nokogiri::HTML(open(url)).xpath("//*")
.xpath("//*[br and text()[string-length(normalize-space()) != 0]]")
.wrap("<span></span>")

puts doc

What your XPath does is it fetches the non-empty text nodes. Which by their very definition don't contain any markup.

In contrast, my XPath fetches any node that contains at least one <br> and at least one non-empty text node.

Tomalak