[Nokogiri-talk] Replace Node w/ Children

Aaron Patterson aaron.patterson at gmail.com
Mon Jun 15 13:28:22 EDT 2009


On Mon, Jun 15, 2009 at 9:54 AM, Trans<transfire at gmail.com> wrote:
> How would I replace a node with it's children?
>
>  node.replace(node.children)
>
> does not work.
>
> In other words I need to "unwrap a node", eg. if I wanted to unwrap <b> in:
>
>  "<a><b><c>C</c></b></a>"
>
> I'd get:
>
>  "<a><c>C</c></a>"
>
> Thanks,

I would do this:

require 'nokogiri'

doc = Nokogiri.XML(DATA.read) { |cfg| cfg.noblanks }

node = doc.at('b')
node.children.each do |child|
  node.parent << child
end
node.remove

puts doc
__END__
<root>
  <a>
    <b>
      <c>C</c>
    </b>
  </a>
</root>

Hope that helps!

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Nokogiri-talk mailing list