Re: xml-to-json() function under SaxonJS
Martin Honnen <[email protected]> Tue, 21 Sep 2021 18:02:48 +0200
| Newsgroups | gmane.text.xml.saxon.help |
|---|---|
| Organization | Liberty Development |
| Message-ID | <[email protected]> |
On 21.09.2021 16:12, Wendell Piez wrote:
> If this is not supported what is the recommended workaround: perhaps a
> JSON serializer is not so hard to write? (I have not thought about it much.)
Another workaround would be to use the XSLT implementation the XSLT 3
spec supplies i.e. given in
https://www.w3.org/TR/xslt-30/#xml-to-json-stylesheet and given at
https://www.w3.org/TR/xslt-30/xml-to-json.xsl.
At least for me with Saxon-JS 2.3, doing
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
xmlns:j="http://www.w3.org/2013/XSLT/xml-to-json"
expand-text="yes">
<xsl:import href="https://www.w3.org/TR/xslt-30/xml-to-json.xsl"/>
<xsl:template match="/*">
<processor-xml-to-json>{xml-to-json(., map { 'indent' : true()
})}</processor-xml-to-json>
<spec-xml-to-json>{j:xml-to-json(., map { 'indent' : true()
})}</spec-xml-to-json>
</xsl:template>
works to give e.g.
<processor-xml-to-json>{"name":"XML","value":"unlimited"}</processor-xml-to-json>
<spec-xml-to-json>{"name" : "XML",
"value" : "unlimited"}</spec-xml-to-json>
so the function is usable and does some job of indentation.