Re: [xsl] is XPath 3.1 xml-to-json() function useful

Subject: Re: [xsl] is XPath 3.1 xml-to-json() function useful
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 8 Mar 2019 11:09:41 -0000
When we were designing this in the WG, we looked at a number of tools and
algorithms for converting generic XML to JSON and found them all to be very
unsatisfactory. In many cases we couldn't even find a decent specification of
what the library did: the only way to find out was by trial and error.
Handling of mixed content, namespaces, content models where some child
elements occur repeatedly and others occur only once, all seemed to cause
considerable problems. In short, we could find any good answer to the problem
of converting *arbitrary* XML to JSON. And we felt that we should only provide
something in the core function library if it was sufficiently generic.

Here's a typical example: http://www.utilities-online.info/xmltojson/ It
converts XML to JSON, but there is NO documentation of the rules it applies.
And if you type in something basic like

<p>This <i>is</i> <i>italic</i> text</p>

you get completely unsatisfactory output:

{
  "p": {
    "#text": [
      "This ",
      " text"
    ],
    "i": [
      "is",
      "italic"
    ]
  }
}
So we decided on a different approach: provide a function to convert a very
specific XML vocabulary to JSON, and then (because we already have powerful
XML-to-XML transformation capabilities), let the user say how their particular
XML should be transformed to that vocabulary.

If there's a particular XML-to-JSON conversion algorithm that you want to
implement, you can implement it as a function library on top of the primitives
provided in the language.

Michael Kay
Saxonica

> On 8 Mar 2019, at 10:49, Mukul Gandhi gandhi.mukul@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi all,
>    I find the XPath 3.1 function json-to-xml() quite useful. It can convert
any JSON input to an XML representation. But I'm not convinced that XPath
3.1's xml-to-json() function (as defined in the spec) is much useful.
>
> Taking an example from XPath 3.1 F&O spec, the following XML document
>
> <array xmlns="http://www.w3.org/2005/xpath-functions
<http://www.w3.org/2005/xpath-functions>"><number>1</number><string>is</strin
g><boolean>1</boolean></array>
>
> converts into following JSON by the function xml-to-json(),
>
> [1,"is",true]
>
> (which is fine)
>
> But as per the XPath 3.1 F&O spec, simple XML documents like,
>
> <root>
>    <val>1</val>
>    <val>2</val>
>    <val>3</val>
>    <val>4</val>
> </root>
>
> cannot be converted into JSON, by the function xml-to-json(), since the XML
input doesn't conform to structure like <array>, <number>, <string> etc (which
looks to me, a very limited capability given to the xml-to-json() function).
>
> Any comments would be useful.
>
>
>
>
> --
> Regards,
> Mukul Gandhi
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

Current Thread