Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nokogiri `add_child` does not copy namespaces
#!/usr/bin/env ruby
# see
require 'nokogiri'
src =<<EOD
<wrapper xmlns="ns">
<record xml:id="r1" xmlns:extra="extra">
<field extra:type="second">bbb</field>
src_doc = Nokogiri::XML(src)
record ='//base:field[. = "bbb"]', {'base' => "ns"})
puts "== Original document"
puts src_doc.to_xml
# if we instead add `record.dup` here, things will work properly
puts "== New document"
puts "== There should be a xmlns declaration in the last field element but it is not"
puts src_doc.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.