w3c / qtspecs

XSLT and XQuery Specifications - the source used to build the specs, and the errata
Other
30 stars 27 forks source link

[xq31] Why don't json-to-xml() and xml-to-json() process JSON as item() ? #34

Open Reino17 opened 2 years ago

Reino17 commented 2 years ago

https://www.w3.org/TR/xpath-functions-31/#func-json-to-xml:

fn:json-to-xml($json-text as xs:string?) as document-node()?
[...]
json-to-xml('{"x": 1, "y": [3,4,5]}')

Why has it been decided to have this function accept the JSON as xs:string?
Intuitively, without reading the documentation, I would've entered json-to-xml(map{"x":1,"y":[3,4,5]}). It's in the name after all.

https://www.w3.org/TR/xpath-functions-31/#func-xml-to-json:

fn:xml-to-json($input as node()?) as xs:string?
[...]
The input <array xmlns="http://www.w3.org/2005/xpath-functions"><number>1</number><string>is</string><boolean>1</boolean></array> produces the result [1,"is",true].

Why has it been decided to have this function put out the JSON as xs:string?
Intuitively, without reading the documentation, I would've expected xml-to-json(<array xmlns="http://www.w3.org/2005/xpath-functions"><number>1</number><string>is</string><boolean>1</boolean></array>) to put out [1,"is",true] as item() (like parse-json()). It's in the name after all.

Why is XML processed as (document-)node(), but JSON not as item()? This...

fn:json-to-xml($json as item()?) as document-node()?

fn:xml-to-json($input as node()?) as item()?

...would've been much more obvious to me.

michaelhkay commented 2 years ago

It's always hard to give a definitive answer to "why" questions, but I suspect the answer can be found in the development timeline.

The two functions were arlready defined in a 2014 draft of XSLT 3.0

https://www.w3.org/TR/2014/WD-xslt-30-20141002/

at which time there was no support in the XDM data model for arrays, and in fact the WG had become very bogged down trying to find an acceptable way to represent arrays in the data model. Direct conversion between the lexical representation of JSON and XML, bypassing any need for maps and arrays, was a solution to this problem. In fact right until the final release of XSLT 3.0 there was a reluctance among some members of the XSL Working Group to make the specification dependent on XPath 3.1, which is where arrays were first supported in the data model.

Michael Kay Saxonica

On 10 Dec 2021, at 13:50, Reino Wijnsma @.***> wrote:

https://www.w3.org/TR/xpath-functions-31/#func-json-to-xml https://www.w3.org/TR/xpath-functions-31/#func-json-to-xml:

fn:json-to-xml($json-text as xs:string?) as document-node()? [...] json-to-xml('{"x": 1, "y": [3,4,5]}')

Why has it been decided to have this function accept the JSON as xs:string? Intuitively, without reading the documentation, I would've entered json-to-xml(map{"x":1,"y":[3,4,5]}). It's in the name after all.

https://www.w3.org/TR/xpath-functions-31/#func-xml-to-json https://www.w3.org/TR/xpath-functions-31/#func-xml-to-json:

fn:xml-to-json($input as node()?) as xs:string? [...] The input 1is1 produces the result [1,"is",true].

Why has it been decided to have this function put out the JSON as xs:string? Intuitively, without reading the documentation, I would've expected xml-to-json(1is1) to put out [1,"is",true] as item() (like parse-json()). It's in the name after all.

Why is XML processed as (document-)node(), but JSON not as item()? This...

fn:json-to-xml($json as item()?) as document-node()?

fn:xml-to-json($input as node()?) as item()? ...would've been much more obvious to me.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/w3c/qtspecs/issues/34, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASIQIUGVGYUTM4X5J3NR73UQIASXANCNFSM5JZF5CJA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Reino17 commented 2 years ago

Thanks for the clarification. Can I then turn this into a request for XPath/XQuery 4.0?