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

Subject: Re: [xsl] is XPath 3.1 xml-to-json() function useful
From: "Wendell Piez wapiez@xxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 8 Mar 2019 22:19:09 -0000
XSL-List,

I can speak to the fact that xml-to-json is extremely useful -- but
you have to think of it in the context of pipelining. It will not be a
simple filter but a sequence of transformations, first, one maps
across from sources into the JSON-as-XML format (which is perfectly
adequate as near as I can tell); the subsequent one produces the
actual JSON. The JSON produced is not the prettiest ever (although I
am no judge) but the model represented is as good as your
transformation makes it. (Having had to do this sort of thing by hand
teaches one to appreciate having a function for it.)

Of course if you don't have consumers demanding JSON, or perhaps there
is an alternative way of producing it that I should consider?

My guess as to the escaped JSON-string output is that you want to use
method='text' not method="json", but I have not tested to confirm.

Indeed, I am very grateful to the WG for working on this whole
living-with-JSON thing (and as always for tool builders), and hope for
the best when it comes to YAML also.

Cheers, Wendell



On Fri, Mar 8, 2019 at 5:07 PM Willem Van Lishout
willemvanlishout@xxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
wrote:
>
> I've been experimenting with the xml-to-json function, but for some reason my output is escaped. Why is that?
>
> Stylesheet:
>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>     xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:f="http://www.w3.org/2005/xpath-functions"; version="3.0">
>     <xsl:output encoding="UTF-8" method="json"/>
>     <xsl:template match="/">
>         <xsl:variable name="transformed">
>             <xsl:apply-templates select="programs"/>
>         </xsl:variable>
>         <xsl:value-of select="xml-to-json($transformed, map{'indent': true()})"/>
>     </xsl:template>
>
>     <xsl:template match="programs">
>         <f:array>
>             <xsl:apply-templates select="program"/>
>         </f:array>
>     </xsl:template>
>
>     <xsl:template match="program">
>         <f:map>
>             <f:string key="title">
>                 <xsl:value-of select="@title"/>
>             </f:string>
>         </f:map>
>     </xsl:template>
> </xsl:stylesheet>
>
> input:
>
> <programs>
>    <program title="ben hur"/>
> </programs>
>
> returns:
>
> "[ \n    { \"title    \" : \"ben hur\" } ]"
>
> Running  this with Saxon 9.8 using Oxygen.
>
>
>
> On Fri, Mar 8, 2019 at 1:00 PM David Maus lists@xxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>
>> On Fri, 08 Mar 2019 12:49:28 +0100,
>> Mukul Gandhi gandhi.mukul@xxxxxxxxx wrote:
>> >
>> > [1  <text/plain; UTF-8 (7bit)>]
>> > [2  <text/html; UTF-8 (quoted-printable)>]
>> > 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.
>>
>> It is very useful to me because it gives full control over the JSON
>> output.
>>
>> At work we recently started to publish descriptions of digital objects
>> as IIIF Manifests [1]. IIIF Manifest is a JSON-based metadata format,
>> the source of the object descriptions is in XML.
>>
>> Example: Manuscript Cod. Guelf. 35 Helmst.
>>
>> XML: https://github.com/dmj/diglib-iiif/blob/master/examples/mss/35-helmst/mets.xml
>> JSON: http://iiif.hab.de/object/mss_35-helmst/manifest.json
>> XSL: https://github.com/dmj/diglib-iiif/blob/master/src/IIIF/Mapper/METS2IIIFv2.xsl
>>
>> Not sure if a 'generic' XML to JSON conversion would be of much
>> help. Being explicit about the JSON structure makes the transformation
>> manageable.
>>
>> Best,
>>   -- David
>>
>> >
>> > Taking an example from XPath 3.1 F&O spec, the following XML document
>> >
>> > <array xmlns="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
>> > EasyUnsubscribe (by email)
>>
>> --
>> David Maus M.A.
>>
>> Www: http://dmaus.name
>> Twitter: @_dmaus
>>
> XSL-List info and archive
> EasyUnsubscribe (by email)



-- 
Wendell Piez | wendellpiez.com | wendell -at- nist -dot- gov
pellucidliterature.org | github.com/wendellpiez |
gitlab.coko.foundation/wendell  - pausepress.org

Current Thread