views:

285

answers:

2

How to replace "foo" to "bar" ?

From

<h1>foo1<p>foo2<a href="foo3.com">foo4</a>foo5</p>foo6</h1>

to

<h1>bar1<p>bar2<a href="foo3.com">bar4</a>bar5</p>bar6</h1>

I want only replace tag inner content, without tag attributes.

Any ideas ?

A: 
require 'rubygems'
require 'nokogiri'

doc = Nokogiri::HTML(DATA)

doc.xpath('//text()').each {|foo|
  dummy = foo.add_previous_sibling(Nokogiri::XML::Node.new("dummy", doc))
  dummy.add_previous_sibling(Nokogiri::XML::Text.new(foo.to_s.gsub(/foo/, "bar"), doc))
  foo.remove
  dummy.remove
}
puts doc

__END__
<h1>foo1<p>foo2<a href="foo3.com">foo4</a>foo5</p>foo6</h1>

I would have thought foo.inner_html.gsub!(/foo/, "bar") works or maybe foo.inner_html = foo.inner_html.gsub(/foo/, "bar"), but it doesn't.
The dummy node is to keep the new text node becoming one with the old text node.

andre-r
Nice, clever aproach
astropanic
A: 

I could do

nokogiri_doc.css('p').each { |p| p.inner_html = p.inner_html.gsub(/\n/, "<br/>") }

To replace all ocurrences of \n inside p tag to br tags

Macario