Re: [xsl] XSLT code for XML to JSON conversion?

Subject: Re: [xsl] XSLT code for XML to JSON conversion?
From: "Piez, Wendell A. (Fed) wendell.piez@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 13 Dec 2019 15:28:06 -0000
Hi,

I think itbs fair to say there will be no absolutely generic XML<->JSON
alignment tool or even approach. This is because there is no such thing as
bgenericb XML or JSON, there is only the actual XML and actual JSON you
actually have or need. As Mike says, the stuff you get from the churner you
find on the Internet just isnbt good enough to use.

That being said, there is a broad need, and this is a research area (see for
example https://www.balisage.net/Proceedings/topics/JSON.html) and projects
are working on it including the one I am involved with these days
(https://pages.nist.gov/OSCAL/) b&

b& which may not be much use to you inasmuch as it is so domain-specific. Our
approach is to produce lossless bidirectional converters, tailored for a
particular application (tag set) from a common metaschema source that bridges
the expressive differences between the two data models. This entails making
certain concessions regarding the modeling, for example, of arbitrary mixed
content. This is something that our domain permits us to do. (bHunks of
proseb are relatively easy to tame and manage in our world, at least
compared with some kinds of data and applications, and where they are not,
there are other standards that can be brought in to help bridge). The flip
side is, we can be much more aggressive than most XML-based documentary
description formats with respect to optimizing both syntax and model on both
sides (i.e., as XML tree of elements vs JSON or YAML objects/properties).
I would say the first question to ask in pondering a conversion pathway is,
how much of your XML is data-like, vs how much of it is bsoupyb
documentary data? In particular, do you have any content models of the form (a
| b)*, which is impossible to represent in JSON without arrays? Note this
family of element structures includes text mixed with bold, italics, and
anchors represented as elements. (Note that recursive models are not a problem
b while maintaining sequence among sibling nodes, is.)

Where there are stretches of mixed element or mixed element-and-text content,
you first of all need a strategy for how that will be dealt with in the JSON.
Without that there is nothing that can be done.

If there are not b if your XML or most of it is already nice and tidy and
data-like, or if you are starting on the up hill side (with the JSON object
model) b then it becomes more conceivable as a mapping exercise.

In either case, starting with a good idea (holistic and comprehensive) of what
the JSON would look like b given the requirements of the data -- is
necessary.

My guess is that there is much experience on this list that might speak to all
this, were it not such an octopus of a topic.

FWIW b the XSLT + XPath 3.1 pathway does work. But it is not generic.

Cheers, Wendell

From: Michael Kay mike@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, December 13, 2019 5:27 AM
To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] XSLT code for XML to JSON conversion?

However, the xml-to-json function on XSLT 3.0 doesn't convert arbitrary XML to
JSON; it requires you first to do a transformation to the XML structure that
reflects the JSON you want to produce.

Generally, off-the-shelf tools that convert arbitrary XML to JSON tend to
produce JSON that's not very nice to work with.

Michael Kay
Saxonica


On 13 Dec 2019, at 10:12, O'Neil Delpratt
oneil@xxxxxxxxxxxx<mailto:oneil@xxxxxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx
rytech.com>> wrote:

Hi,

Saxon on the Java Platform would work fine on the command-line, but as Liam
said you have the Java runtime startup.

Alternatively Saxon/C could equally fit your requirements. It is cross
compiled to native library, which can be run on the command-line using C/C++.
Saxon/C also comes with Python extension API.

kind regards,

O'Neil



On 12 Dec 2019, at 22:37, Steven D. Majewski
steve.majewski@xxxxxxxxx<mailto:steve.majewski@xxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx
rytech.com>> wrote:

There are also non XSLT converters that use SAX: read XML and serialize out as
JSON.

There are several node-js packages.
Python xmljson.
https://pypi.org/project/xmljson/<https://gcc01.safelinks.protection.outlook.
com/?url=https%3A%2F%2Fpypi.org%2Fproject%2Fxmljson%2F&data=02%7C01%7Cwendell
.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054
655c61dec%7C1%7C1%7C637118296523829713&sdata=8VEyzjMr%2F6AgJgC4XTVNnicA8n9TaV
G0HLynLOju%2BNA%3D&reserved=0> supports several different JSON encoding
conventions.

And Ibm sure many others.
( Ibve used the Python package, but not for any large projects.  )

b Steve.



On Dec 12, 2019, at 5:09 PM, Liam R. E. Quin
liam@xxxxxxxxxxxxxxxx<mailto:liam@xxxxxxxxxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx
rytech.com>> wrote:

On Thu, 2019-12-12 at 21:32 +0000, Richard Fozzard
richard.fozzard@xxxxxxxx<mailto:richard.fozzard@xxxxxxxx> wrote:

Hi Folks,

We're looking to do conversion of relatively complex XML (i.e.
namespaces, attributes, repeating elements, xlinks) into JSON from
the
Unix command line to build a web page. We've  used the XML to JSON
feature in the Oxygen Developer tool, and it works well, but that's
a
GUI, not a command line tool.

It's probably using XSLT under the hood, with a stylesheet that's
usable e.g. with Saxon. You do pay the penalty of Java runtime startup,
though.

Note also that XSLT 3 includes a standard function to produce JSON; see
https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping<https://gcc01.s
afelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.w3.org%2FTR%2Fxpath-fu
nctions-31%2F%23json-to-xml-mapping&data=02%7C01%7Cwendell.piez%40nist.gov%7C
27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7
C637118296523829713&sdata=d9X42Ia%2BxD51xFDGu3oUWavZaqemv33M6OLVxbPdTOw%3D&re
served=0>

So you could just use xml-to-json() in XSLT. You may need Saxon EE for
that, i'm not sure offhand.

Of course, you could also just send XML to the Web client; what you are
doing by using a complex transformation into JSON is moving the
complexity from one developer to another. You could also generate HTML
on the server, whic might make it easier to meet 508/WCAG requirements
:)

Liam

--
Liam Quin,
https://www.delightfulcomputing.com/<https://gcc01.safelinks.protection.outlo
ok.com/?url=https%3A%2F%2Fwww.delightfulcomputing.com%2F&data=02%7C01%7Cwende
ll.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e0
54655c61dec%7C1%7C1%7C637118296523839707&sdata=Xz9zatcz1n6AHAqoh7XQDsYIkkynGv
0pcDsCYCVIupc%3D&reserved=0>
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations:
http://www.fromoldbooks.org<https://gcc01.safelinks.protection.outlook.com/?u
rl=http%3A%2F%2Fwww.fromoldbooks.org%2F&data=02%7C01%7Cwendell.piez%40nist.go
v%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7
C1%7C637118296523839707&sdata=j51fecHrRP%2BAOYDbAQl51lMSH%2FDQKySEsLJ%2BR6Pz5
gE%3D&reserved=0>

XSL-List info and
archive<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.
mulberrytech.com%2Fxsl%2Fxsl-list&data=02%7C01%7Cwendell.piez%40nist.gov%7C27
ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C6
37118296523849705&sdata=GZAWBLlVXtPQjotdXzWBFo0YYboAzNih8g0iRgwa3bI%3D&reserv
ed=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2
F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F1363285&data=02%7C01%7Cwendel
l.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e05
4655c61dec%7C1%7C1%7C637118296523849705&sdata=FEMF6d6fKaap2nMGfHTK4jqkpnKjrZi
wW0L0gKtqyrQ%3D&reserved=0> (by email)

XSL-List info and
archive<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.
mulberrytech.com%2Fxsl%2Fxsl-list&data=02%7C01%7Cwendell.piez%40nist.gov%7C27
ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C6
37118296523859695&sdata=0bDpX8hB%2BajrSRAuGb1LdPvRYz7UR4Wy2frxdzU%2F9iE%3D&re
served=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2
F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F293509&data=02%7C01%7Cwendell
.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054
655c61dec%7C1%7C1%7C637118296523859695&sdata=o4e%2B53RBFUpNnoBN1Pe09BtuTsH8O2
DpggPiQdVkmzk%3D&reserved=0> (by email)

XSL-List info and
archive<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.
mulberrytech.com%2Fxsl%2Fxsl-list&data=02%7C01%7Cwendell.piez%40nist.gov%7C27
ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C6
37118296523869693&sdata=8X%2FCsVYbq64YRTXhB%2BDj%2F249Eo3F7wZdEfbIaoybpAM%3D&
reserved=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2
F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F3302254&data=02%7C01%7Cwendel
l.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e05
4655c61dec%7C1%7C1%7C637118296523879686&sdata=pj6vQWoQ5twtNmVRH7hFD9GWUU7qdQV
3%2F5%2BxNLhT1IY%3D&reserved=0> (by email<>)

Current Thread