title: How to create an XML document with a namespaced root element with Nokogiri Builder
body: <p>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 a root node with a namespace.</p>
  
  <p>This works:</p>
  
  <pre><code>Nokogiri::XML::Builder.new { |xml| xml.root('xmlns:foobar' =&gt; 'my-ns-url') }.to_xml
  
  &lt;?xml version="1.0"?&gt;
  &lt;root xmlns:foobar="my-ns-url"/&gt;
  </code></pre>
  
  <p>As does this:</p>
  
  <pre><code>Nokogiri::XML::Builder.new do |xml| 
    xml.root('xmlns:foobar' =&gt; 'my-ns-url') { xml['foobar'].child }
  end.to_xml
  
  &lt;?xml version="1.0"?&gt;
  &lt;root xmlns:foobar="my-ns-url"&gt;
    &lt;foobar:child/&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>However, I need something like <code>&lt;foo:root&gt;</code> and this doesn't work:</p>
  
  <pre><code>Nokogiri::XML::Builder.new { |xml| xml['foobar'].root('xmlns:foobar' =&gt; 'my-ns-url') }.to_xml
  
  NoMethodError: undefined method `namespace_definitions' for #&lt;Nokogiri::XML::Document:0x11bfef8 name="document"&gt;
  </code></pre>
  
  <p>Namespaces have to be defined before use, apparently, so there's no way to add one to the root node. </p>
  
  <p>I found "<a href="http://groups.google.com/group/nokogiri-talk/browse_thread/thread/25aa31b0ff5c9694/909117aa559b88f1?lnk=gst&amp;q=namespace+root#909117aa559b88f1" rel="nofollow noreferrer">Define root node with a namespace?</a>" on the Nokogiri mailing list, but it had no replies. </p>
  
  <p>Does anyone have a solution?</p>