rubyhacker / gmail.com wrote:
> Contrived example below in case it helps.
input = <<ENDHTML
<html>
<body>
<p>Hi, there.</p>
<foo bar="this" bam="that">some more text</foo>
<p>That's all.</p>
</body>
</html>
ENDHTML
require 'rexml/document'
doc = REXML::Document.new( input )
doc.root.each_element( '//foo' ){ |e|
new_para = REXML::Element.new( 'p' )
new_para.text = "I found a foo tag enclosing '#{e.text}' with bar and
bam values of '#{e.attributes['bar']}' and '#{e.attributes['bam']}'..."
e.parent.replace_child( e, new_para )
}
puts doc
#=> <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>