Skip to content

Instantly share code, notes, and snippets.

@gioele
Created November 25, 2014 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gioele/2c88ac73f4f28f79fbc6 to your computer and use it in GitHub Desktop.
Save gioele/2c88ac73f4f28f79fbc6 to your computer and use it in GitHub Desktop.
Nokogiri `add_child` does not copy namespaces
#!/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)
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