Skip to content

Instantly share code, notes, and snippets.

@kbrock
Forked from gioele/add_child_ns.rb
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbrock/468f9f5e987e3b60155b to your computer and use it in GitHub Desktop.
Save kbrock/468f9f5e987e3b60155b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'nokogiri'
src =<<EOD
<wrapper xmlns="ns" xmlns:extra="extra">
<record xml:id="r1">
<field>aaa</field>
<field extra:type="second">bbb</field>
</record>
</wrapper>
EOD
src_doc = Nokogiri::XML(src)
record = src_doc.at('//base:record', {'base' => "ns"})
puts "== Original document"
puts src_doc.to_xml
dest_doc = Nokogiri::XML("<summary xmlns='summary'/>")
dest_doc.root.add_child(record.clone)
puts
puts "== New document"
puts "== The attribute `type` should be in namespace `extra`, but it is not"
puts dest_doc.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment