Swapping tags & deleting tags via nokogiri

let’s say I have an xml document like this

some text blah blah

is there a simple method in nokogiri to:

step 1: convert all tags to something else (e.g.,

)
creating this…



some text blah blah



step 2: remove all tags but keep the internal tags, creating
this


some text blah blah


I imagine that step 2 is done all the time and there’s probably a
simple way to do it, but I’m not sure where to begin with either.

file = Nokogiri::XML(File.new(file_name)) # <= this is about as far
as I’ve gotten (not very…)

Thanks!!

Check out Loofah: GitHub - flavorjones/loofah: Ruby library for HTML/XML transformation and sanitization


frmsrcurl:
http://compgroups.net/comp.lang.ruby/swapping-tags-deleting-tags-via-nokogiri

On Tue, Dec 15, 2009 at 11:55 PM, pablitoman [email protected]
wrote:

I imagine that step 2 is done all the time and there's probably a simple way to do it, but I'm not sure where to begin with either.

file = Nokogiri::XML(File.new(file_name)) # <= this is about as far
as I’ve gotten (not very…)

Try Loofah, which is based on Nokogiri:

require “loofah”

xml = <<-eoxml



some text blah blah



eoxml

doc = Loofah.xml_document(xml)
scrubber = Loofah::Scrubber.new do |node|
node.name = “h1” if node.name == “abcde”
if node.name == “fghij”
node.before node.inner_html
node.remove
end
end
doc.scrub!(scrubber)
puts doc.to_s

=> <?xml version="1.0"?>

some text blah blah