Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > swapping tags & deleting tags via nokogiri

Reply
Thread Tools

swapping tags & deleting tags via nokogiri

 
 
pablitoman
Guest
Posts: n/a
 
      12-16-2009

let's say I have an xml document like this

<document>
<abcde>
<fghij>
some text blah blah
</fghij>
</abcde>
</document>

is there a simple method in nokogiri to:

step 1: convert all <fghij> tags to something else (e.g., <h1>)
creating this…
<document>
<abcde>
<h1>
some text blah blah
</h1>
</abcde>
</document>

step 2: remove all <abcde> tags but keep the internal tags, creating
this
<document>
<h1>
some text blah blah
</h1>
</document>


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!!
 
Reply With Quote
 
 
 
 
Mike Dalessio
Guest
Posts: n/a
 
      12-16-2009
On Tue, Dec 15, 2009 at 11:55 PM, pablitoman <(E-Mail Removed)> wrote:

>
> let's say I have an xml document like this
>
> <document>
> <abcde>
> <fghij>
> some text blah blah
> </fghij>
> </abcde>
> </document>
>
> is there a simple method in nokogiri to:
>
> step 1: convert all <fghij> tags to something else (e.g., <h1>)
> creating this=85
> <document>
> <abcde>
> <h1>
> some text blah blah
> </h1>
> </abcde>
> </document>
>
> step 2: remove all <abcde> tags but keep the internal tags, creating
> this
> <document>
> <h1>
> some text blah blah
> </h1>
> </document>
>
>
> 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 =3D Nokogiri::XML(File.new(file_name)) # <=3D this is about as far
> as I've gotten (not very...)
>
>

Try Loofah, which is based on Nokogiri:

require "loofah"

xml =3D <<-eoxml
<document>
<abcde>
<fghij>
some text blah blah
</fghij>
</abcde>
</document>
eoxml

doc =3D Loofah.xml_document(xml)
scrubber =3D Loofah::Scrubber.new do |node|
node.name =3D "h1" if node.name =3D=3D "abcde"
if node.name =3D=3D "fghij"
node.before node.inner_html
node.remove
end
end
doc.scrub!(scrubber)
puts doc.to_s
# =3D> <?xml version=3D"1.0"?>
# <document>
# <h1>
# some text blah blah
# </h1>
# </document>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
[ANN] nokogiri 1.0.0 Released Aaron Patterson Ruby 0 10-31-2008 01:31 AM
Deleting element tags Rafal Konopka Perl Misc 0 01-04-2007 02:22 AM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
Deleting tags via XSL Thomas Vackier XML 5 12-15-2003 08:48 AM



Advertisments