Sunday 15 August 2010

ruby - Creating an XML document with a namespaced root element with Nokogiri builder -


I am implementing an exporter for XML data format which requires namespace. I am using builder (version 1.4.0).

However, I can not find the nockery to create a root node with a namespace.

This works:

  NotchGree :: XML :: Builder. NY || Xml | Xml.root ('xmlns: foobar' = & gt; 'my-ns-url')} .to_xml & lt ;? XML version = "1.0"? & Gt; & Lt; Root xmlns: foobar = "my-ns-url" />  

does this:

  Nokogiri :: XML :: Builder. NUE | 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;  

However, give me & lt; Foo: root & gt; Like anything else it will not work:

  Nokogiri :: XML :: Builder.new {| Xml | | Xml ['foobar']. Root ('xmlns: foobar' = & gt; 'my-ns-url')} .to_xml no. Method: unknown method # namespace_definitions for # nokogiri :: XML :: document: 0x11bfef8name = "document" & gt;  

Namespaces should be defined before using, apparently, there is no way to add one to the root node.

I found, but had no answer, did anyone have any solutions?

  'rubygems' is required 'nokogiri' is required Nokogiri :: XML :: builder.nu {| Xml | Xml.root ("xmlns: foo" = & gt; "url") {xml.parent.namespace = xml.parent.namespace_definitions.find {| Ns | Ns.prefix == "foo"} xml ['foo']. Child}} .to_xml  

You can not use it before the namespace is defined xml ['foo'] that means it is root node. Thus, after the root-node, add the above named name after the namespace.


No comments:

Post a Comment