why the lucky stiff
8/22/2006 11:09:00 PM
On Wed, Aug 23, 2006 at 07:15:09AM +0900, rubyhacker@gmail.com wrote:
> Input:
>
> <html>
> <body>
> <p>Hi, there.</p>
> <foo bar="this" bam="that">some more text</foo>
> <p>That's all.</p>
> </body>
> </html>
>
> Output:
> <html>
> <body>
> <p>Hi, there.</p>
> <p>I found a foo tag enclosing 'some more text' with
> bar and bam values of 'this' and 'that'...</p>
> <p>That's all.</p>
> </body>
> </html>
So, in Hpricot:
doc = Hpricot("<html>...</html>")
doc.search("foo").each do |ele|
new_ele = Hpricot '<p>I found a ' + ele.name + " tag enclosing \'" +
ele.inner_html + "' with " + ele.attributes.keys.join(' and ') +
" values of " + ele.attributes.values.map { |x| "'#{x}'" }.join(' and ') +
"...</p>"
ele.parent.replace_child(ele, new_ele.children.first)
end
puts doc
REXML has a replace_child as well. But now you've motivated me to add Element#replace.
_why