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

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 and projects
are working on it including the one I am involved with these days
( 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

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

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


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,


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

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

There are several node-js packages.
Python xmljson.<
G0HLynLOju%2BNA%3D&reserved=0> supports several different JSON encoding

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
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx>> 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
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
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,

Note also that XSLT 3 includes a standard function to produce JSON; see<https://gcc01.s

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 Quin,<
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations:<

XSL-List info and
wW0L0gKtqyrQ%3D&reserved=0> (by email)

XSL-List info and
DpggPiQdVkmzk%3D&reserved=0> (by email)

XSL-List info and
3%2F5%2BxNLhT1IY%3D&reserved=0> (by email<>)

Current Thread