xsl-list
[Top] [All Lists]

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

2019-12-13 09:28:12
Hi,

I think it’s 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 
“generic” 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 isn’t 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/) …

… 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. (‘Hunks of prose’ 
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 “soupy” 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 – 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 – 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) – 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 – 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 – the XSLT + XPath 3.1 pathway does work. But it is not generic.

Cheers, Wendell

From: Michael Kay mike(_at_)saxonica(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Friday, December 13, 2019 5:27 AM
To: xsl-list <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
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(_at_)saxonica(_dot_)com<mailto:oneil(_at_)saxonica(_dot_)com> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)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(_dot_)majewski(_at_)gmail(_dot_)com<mailto:steve(_dot_)majewski(_at_)gmail(_dot_)com>
 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)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%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523829713&sdata=8VEyzjMr%2F6AgJgC4XTVNnicA8n9TaVG0HLynLOju%2BNA%3D&reserved=0>
 supports several different JSON encoding conventions.

And I’m sure many others.
( I’ve used the Python package, but not for any large projects.  )

— Steve.



On Dec 12, 2019, at 5:09 PM, Liam R. E. Quin 
liam(_at_)fromoldbooks(_dot_)org<mailto:liam(_at_)fromoldbooks(_dot_)org> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>>
 wrote:

On Thu, 2019-12-12 at 21:32 +0000, Richard Fozzard
richard(_dot_)fozzard(_at_)noaa(_dot_)gov<mailto:richard(_dot_)fozzard(_at_)noaa(_dot_)gov>
 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.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.w3.org%2FTR%2Fxpath-functions-31%2F%23json-to-xml-mapping&data=02%7C01%7Cwendell.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637118296523829713&sdata=d9X42Ia%2BxD51xFDGu3oUWavZaqemv33M6OLVxbPdTOw%3D&reserved=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.outlook.com/?url=https%3A%2F%2Fwww.delightfulcomputing.com%2F&data=02%7C01%7Cwendell.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523839707&sdata=Xz9zatcz1n6AHAqoh7XQDsYIkkynGv0pcDsCYCVIupc%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/?url=http%3A%2F%2Fwww.fromoldbooks.org%2F&data=02%7C01%7Cwendell.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523839707&sdata=j51fecHrRP%2BAOYDbAQl51lMSH%2FDQKySEsLJ%2BR6Pz5gE%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%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523849705&sdata=GZAWBLlVXtPQjotdXzWBFo0YYboAzNih8g0iRgwa3bI%3D&reserved=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F1363285&data=02%7C01%7Cwendell.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523849705&sdata=FEMF6d6fKaap2nMGfHTK4jqkpnKjrZiwW0L0gKtqyrQ%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%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523859695&sdata=0bDpX8hB%2BajrSRAuGb1LdPvRYz7UR4Wy2frxdzU%2F9iE%3D&reserved=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F293509&data=02%7C01%7Cwendell.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523859695&sdata=o4e%2B53RBFUpNnoBN1Pe09BtuTsH8O2DpggPiQdVkmzk%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%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523869693&sdata=8X%2FCsVYbq64YRTXhB%2BDj%2F249Eo3F7wZdEfbIaoybpAM%3D&reserved=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F3302254&data=02%7C01%7Cwendell.piez%40nist.gov%7C27ced4f6cd9a4c4b685608d77fb70f62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637118296523879686&sdata=pj6vQWoQ5twtNmVRH7hFD9GWUU7qdQV3%2F5%2BxNLhT1IY%3D&reserved=0>
 (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>