xsl-list
[Top] [All Lists]

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

2019-03-08 05:09:47
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(_dot_)mukul(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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</string><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 <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>