Convert Between JSON and XML from XSLT
Two XPath extension functions are available as built-in Oxygen functions for converting between JSON and XML formats from an XSLT file:
-
xs:string xml-to-json(xs:string jsonText) - Converts the input
jsonTextto XML, using the JSON to XML conversion tool provided by Oxygen XML Editor. -
xs:string json-to-xml(xs:string xmlText) - Converts the input
xmlTextto JSON, using the XML to JSON conversion tool provided by Oxygen XML Editor.Tip:These functions are available in the list of content completion proposals when editing XPath expressions in XSLT 3.0 (or later).
The namespace of the functions is:
xmlns:oxy=http://www.oxygenxml.com/extension (content completion is
available for the xmlns:oxy prefix).
Examples of XSLT files that perform a conversion:
XML to JSON
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oxy="http://www.oxygenxml.com/extension" version="3.0">
<xsl:output method="text" indent="false" omit-xml-declaration="true"/>
<xsl:template match="/">
<xsl:value-of select="oxy:xml-to-json(serialize(node()))"/>
</xsl:template>
</xsl:stylesheet>
JSON to XML
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oxy="http://www.oxygenxml.com/extension" version="3.0">
<xsl:template match=".">
<xsl:variable name="xmlContent"
select="oxy:json-to-xml(serialize(., map { 'method' : 'json', 'indent' : true() }))"/>
<xsl:value-of select="$xmlContent" disable-output-escaping="true"/>
</xsl:template>
</xsl:stylesheet>