Skip to content

What does casting from JSON to XML really mean? #63

@ndw

Description

@ndw

Consider this pipeline:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:cx="http://xmlcalabash.com/ns/extensions"
                xmlns:ex="http://example.com/"
                name="main" version="3.0">
  <p:output port="result"/>

  <p:variable name="data" select="/*">
    <p:inline>
      <data><value type="number">5</value></data>
    </p:inline>
  </p:variable>

  <p:variable name="json" xmlns:ex2="http://example.com/"
                          xmlns:ex3="http://example.com/3"
              select='map { xs:QName("cx:spoon"): "map key",
                            "map-value": xs:QName("ex:fork"),
                            "array": array { 1, 2, xs:QName("cx:knife") },
                            "array2": array { 1, map { xs:QName("ex2:chopstick"): true() } },
                            "Q{http://example.com/}test": (),
                            QName("http://example.com/", "cx:badns"): false(),
                            "xml": $data
                          }'/>

  <p:identity>
    <p:with-input select="$json">
      <irrelevant/>
    </p:with-input>
  </p:identity>

  <p:cast-content-type content-type="application/xml"/>
</p:declare-step>

We use the content type application/json for atomic values, maps, and arrays, but that's kind of a lie. What we really have are XDM atomic values, maps, and arrays which support more data types than JSON. What is this pipeline supposed to produce? My naive implementation of casting from JSON to XML serializes the JSON then calls fn:json-to-xml. The result is:

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="map-value">ex:fork</string>
   <boolean key="cx:badns">false</boolean>
   <null key="Q{http://example.com/}test"/>
   <string key="xml">&lt;data xmlns:cx="http://xmlcalabash.com/ns/extensions" xmlns:ex="http://example.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;&lt;value type="number"&gt;5&lt;/value&gt;&lt;/data&gt;</string>
   <array key="array2">
      <number>1</number>
      <map>
         <boolean key="ex2:chopstick">true</boolean>
      </map>
   </array>
   <array key="array">
      <number>1</number>
      <number>2</number>
      <string>cx:knife</string>
   </array>
   <string key="cx:spoon">map key</string>
</map>

I guess that's the right answer. There's no where in the XML vocabulary to hang additional information about the fact that some of those keys are QNames or that the value of one of the keys is XML.

But it's not very satisfying if you've got, for example, document properties that have QName keys and you want to turn them into XML.

I wonder if we should think about defining some other JSON-to-XML casting in V.next?

And I wonder if we should add a note to the current spec about this lossy transformation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions