lutaml / shale

Shale is a Ruby object mapper and serializer for JSON, YAML, TOML, CSV and XML. It allows you to parse JSON, YAML, TOML, CSV and XML data and convert it into Ruby data structures, as well as serialize data structures into JSON, YAML, TOML, CSV or XML.
https://shalerb.org/
MIT License
0 stars 1 forks source link

Update README for allowing default XML namespaces and add tests #11

Closed ronaldtse closed 2 months ago

ronaldtse commented 2 months ago

Update README for allowing default XML namespaces.

Roundtrip test as contributed by @opoudjis:

require 'shale'
class Person  < ::Shale::Mapper
  require 'shale/adapter/nokogiri'
  ::Shale.xml_adapter = ::Shale::Adapter::Nokogiri

  attribute :output1, ::Shale::Type::String
  attribute :output2, ::Shale::Type::String
  xml do
    root 'collection'
    namespace 'http://metanorma.org', nil
    map_attribute 'output1', to: :output1
    map_element 'output2', to: :output2
  end
end

person = Person.from_xml(<<~DATA)
  <collection xmlns="http://metanorma.org" output1='A'>
    <output2>John</output2>
    </collection>
DATA

pp person.to_xml

Originally posted by @HassanAkbar in https://github.com/lutaml/shale/issues/6#issuecomment-2134612796