Created
November 25, 2014 18:42
-
-
Save gioele/2c88ac73f4f28f79fbc6 to your computer and use it in GitHub Desktop.
Nokogiri `add_child` does not copy namespaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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