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

`schemaLocation` is not really supported #3

Open ronaldtse opened 3 months ago

ronaldtse commented 3 months ago

In XML files there are many occasions that alongside an XML namespace declaration, a schemaLocation is also provided.

e.g.

         -<?xml version="1.0" encoding="UTF-8"?>
         -<gml:Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml/3.2" xsi:schemaLocation="http://www.opengis.net/gml http://schemas.opengis.net/gml/3.1.1/profiles/SimpleDictionary/1.0.0/gmlSimpleDictionaryProfile.xsd" gml:id="cl_e3c8f730-a2a0-4883-bf3c-1c8dcefdf693">
         +<?xml version="1.0" encoding="utf-8"?>
         +<gml:Dictionary xmlns:gml="http://www.opengis.net/gml/3.2" gml:id="cl_e3c8f730-a2a0-4883-bf3c-1c8dcefdf693">

Notice that in the original <gml:Dictionary> it contains schemaLocation. However, in Shale I have to manually encode schemaLocation as a data attribute, which is quite crazy.

ronaldtse commented 3 months ago

The workaround is actually this:

class SomeClass < Shale::Mapper
  attribute :schema_location, Shale::Type::String

  xml do
    root "Dictionary"

    map_attribute "schemaLocation", to: :schema_location, prefix: "xsi", namespace: "http://www.w3.org/2001/XMLSchema-instance"
end