I\'m implementing an exporter for XML data that requires namespaces. I\'m using Nokogiri\'s XML Builder (version 1.4.0) to do this, however, I can\'t get Nokogiri to create
require 'rubygems'
require 'nokogiri'
puts Nokogiri::XML::Builder.new do |xml|
xml.root("xmlns:foo"=>"url") do
xml.parent.namespace = xml.parent.namespace_definitions.find{|ns|ns.prefix=="foo"}
xml['foo'].child
end
end.to_xml
You cannot use xml['foo'] before the namespace is defined, I.E. before you pass it as an argument to the root node, thus, the code above added the namespace after-the-fact to the root node.