Re: JSON: another option for the format

Liam,

> Have you looked at the JSOn to XML mapping used by XSLT 3 and XQuery
3.1?

Yes we have looked at the XSLT 3 mapping. The XForms mapping is
different in that it starts with the idea that you can write XPath
against an XML data model created from JSON in a more natural way.
Take the example in the XSLT 3 spec:

    {
      "desc"    : "Distances between several cities, in kilometers.",
      "updated" : "2014-02-04T18:50:45",
      "uptodate": true,
      "author"  : null,
      "cities"  : {
        "Brussels": [
          {"to": "London",    "distance": 322},
          {"to": "Paris",     "distance": 265},
          {"to": "Amsterdam", "distance": 173}
        ],
        "London": [
          {"to": "Brussels",  "distance": 322},
          {"to": "Paris",     "distance": 344},
          {"to": "Amsterdam", "distance": 358}
        ],
        "Paris": [
          {"to": "Brussels",  "distance": 265},
          {"to": "London",    "distance": 344},
          {"to": "Amsterdam", "distance": 431}
        ],
        "Amsterdam": [
          {"to": "Brussels",  "distance": 173},
          {"to": "London",    "distance": 358},
          {"to": "Paris",     "distance": 431}
        ]
      }
    }

And say I want to access the distance in `{"to": "Paris",     "distance": 344}`.

With the XForms mapping I write, assuming the context item is the root element:

    cities/London[2]/distance

or, as I suggest with my change to the mapping:

    cities/London/_[2]/distance

With the XSLT 3 mapping, I would need to write:

    map[@key = 'cities']/array[@key = 'London']/map[2]/number[@key = 'distance']

or, to optimize for concision:

    *[@key = 'cities']/*[@key = 'London']/*[2]/*[@key = 'distance']

> In particular, what do you do about cases like { "< <": [[ "1 2 3" ] { "_" : 6 } ]} ?

These cases are handled by replacing illegal character with `_` and
adding the `@name` attribute on the element to expose the original
name. For example with the mapping as it stands:

    <json object="true">
      <___ array="true" name="&lt; &lt;">
          <_ name="" array="true" type="string">1 2 3</_>
      </___>
      <___ array="true" name="&lt; &lt;" object="true">
          <_ type="number">6</_>
      </___>
    </json>

(The result would be slightly different with the update to the mapping
I am suggesting above.)

The XForms mapping optimizes paths for the most common scenarios where
property names are "reasonable", while supporting the less frequent
cases where property names which contain characters not allowed in XML
elements. For those, XPath expressions won't look as clean.

-Erik

Received on Monday, 23 November 2015 18:13:40 UTC