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

XML output variants #9

Closed andrew2net closed 2 months ago

andrew2net commented 2 months ago

Relaton has two slightly different XML representations of BibliongraphicItem. These representations have different root elements: bibitem and bibdata. We can make mappings for each XML representation but I don't see how to create BibliographicItem from bibitem and then render bibdata output, or vice versa. Our BibliograpnicItem can output both XML variants, but with Shale we can only create Bibitem class from bibitem element and Bibdata class from bibdata element. Any ideas?

ronaldtse commented 2 months ago

@andrew2net whats the reason we use different names?

One way to do it is to inherit one class from another class and they share the same content but just a different root?

opoudjis commented 2 months ago

The reason is that they do different things in Metanorma: metadata of a document vs bibliographical item within a document.

andrew2net commented 2 months ago

@ronaldtse The problem is when Shale parses bibitem it creates an object of Bibitem, and the object can render only bibitem, it can't render anything else. I think we can use BibliographicItem as a custom model, so when we parse bibitem or bibdata we can get the custom model from them, and to render output we should create Bibitem or Bibdate mapper using BibliographicItem's attributes. I'll try this approach, and close this issue if it works.

ronaldtse commented 2 months ago

@andrew2net how did you resolve this issue?

Did this approach work for you?

class BibData
  attribute '...'
  xml do
    root 'bibdata'
    map_attribute '...'
  end
end

class BibItem < BibData
  xml do
    root 'bibitem'
    map_attribute '...'
  end
end
andrew2net commented 2 months ago

@ronaldtse I found out that if a custom model is used, it's possible to render an item with Bibitem.to_xml(item) or Bibdata.to_xml(item), whatever we need.