mvz / happymapper

Object to XML mapping library, using Nokogiri (Fork from John Nunemaker's Happymapper)
http://github.com/mvz/happymapper/
MIT License
151 stars 44 forks source link

to_xml: How to Add a Namespace to the Root Element Only #223

Open sshaw opened 1 year ago

sshaw commented 1 year ago

In most cases the namespace only applies to the root element but outputting this sort of XML does not seem possible. For example, given this schema one must output the following:

<ns1:Transmission xmlns:ns1="https://ldex.limra.com/xsd/1.0/LDExBEM">
  <TransmissionGUID>1234106c-d731-466a-8235-3377985285yy</TransmissionGUID>
  <!-- ... -->
</ns1:Transmission>

~Now~ How to achieve this? The namespace method is used "if a node and all its children are all namespaced elements".

The tag method does not accept a namespace argument like element method does. Specifying:

element :transmission_guid, String, :tag => "TransmissionGUID", :namespace => nil

or :namespace => false does not prevent the namespace provided to namespace from being output.

Does not seem possible to output a namespace only on the root element.

yvesll commented 1 year ago

Similar scenario in #217, if you use MRI ruby, you can just define a tag with namespace like:

tag 'ns1:Transmission'
sshaw commented 1 year ago

Thanks @yvesll but that is just an accident and will be fixed soon so not safe to use long-term.