Closed martinnicolas closed 1 year ago
Yeah, that would be a nice addition, unfortunately that's not something Shale controls. How the rendering is done is entirely dependent on the underlaying XML parser, or event parser version/architecture (e.g. I ran into issues where generated XML was different on Mac OS and Linux when using Nokogiri).
You can try to use different parser, e.g. REXML and Ox allow to control how the nodes are rendered by using nil
or empty string ''
e.g.
require 'shale'
require 'shale/adapter/rexml'
Shale.xml_adapter = Shale::Adapter::REXML
class Person < Shale::Mapper
attribute :first_name, Shale::Type::String
attribute :last_name, Shale::Type::String
xml do
root 'person'
map_element 'first_name', to: :first_name, render_nil: true
map_element 'last_name', to: :last_name, render_nil: true
end
end
person = Person.new(first_name: '', last_name: nil)
puts person.to_xml(pretty: true)
This will render:
<person>
<first_name></first_name>
<last_name/>
</person>
Nice!. I will try these alternatives. Thanks!.
Some times we need to define an xml tag without content inside like this:
Currently, shale is closing xml tags without content like this:
Im fixing this mapping content to empty string. Like this
and this
It could be a nice improvement to provide an option to choose how to close tags without content!.