Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ndw opened this issue Jan 31, 2025 · 1 comment
Open

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

ndw opened this issue Jan 31, 2025 · 1 comment

Comments

@ndw
Copy link

ndw commented Jan 31, 2025

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.

@ndw
Copy link
Author

ndw commented Jan 31, 2025

@xml-project says that Oxygen has a better conversion. We (implementors) could add support for that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant