swapping tags & deleting tags via nokogiri

Discussion in 'Ruby' started by pablitoman, Dec 16, 2009.

  1. pablitoman

    pablitoman Guest

    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!!
    pablitoman, Dec 16, 2009
    #1
    1. Advertising

  2. On Tue, Dec 15, 2009 at 11:55 PM, pablitoman <> 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>
    Mike Dalessio, Dec 16, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hrvoje Vrbanc
    Replies:
    0
    Views:
    330
    Hrvoje Vrbanc
    Feb 25, 2004
  2. Thomas Vackier

    Deleting tags via XSL

    Thomas Vackier, Dec 12, 2003, in forum: XML
    Replies:
    5
    Views:
    505
    Thomas Vackier
    Dec 15, 2003
  3. Harry Barker
    Replies:
    2
    Views:
    502
    Alf P. Steinbach
    Apr 19, 2006
  4. Rafal Konopka

    Deleting element tags

    Rafal Konopka, Jan 4, 2007, in forum: Perl Misc
    Replies:
    0
    Views:
    89
    Rafal Konopka
    Jan 4, 2007
  5. crea
    Replies:
    2
    Views:
    394
    Nobody
    Dec 28, 2012
Loading...

Share This Page