Convert JSON to XML and XML to JSON in XSLT 3.0

XSLT 3.0 introduces native support for reading, manipulating, and outputting JSON data without relying on third-party preprocessors or custom parsing libraries. By utilizing built-in functions such as fn:json-to-xml() and fn:xml-to-json(), developers can seamlessly convert JSON strings into structured XML document trees and serialize structured XML directly into valid JSON strings.

Converting JSON directly to XML with fn:json-to-xml()

The fn:json-to-xml() function accepts a JSON-encoded string and converts it into an XDM node tree representation using the standard namespace http://www.w3.org/2005/xpath-functions.

Example Usage

<xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    expand-text="yes">

    <xsl:param name="json-input" as="xs:string">
        {
            "title": "XSLT 3.0 Guide",
            "pages": 120,
            "published": true
        }
    </xsl:param>

    <xsl:template name="xsl:initial-template">
        <xsl:variable name="xml-representation" select="json-to-xml($json-input)"/>
        <xsl:copy-of select="$xml-representation"/>
    </xsl:template>
</xsl:stylesheet>

The Resulting XML Format

The output follows the standard XSLT JSON-XML vocabulary: * Objects map to <fn:map> * Arrays map to <fn:array> * Key-value pairs define a @key attribute * Atomic values map to <fn:string>, <fn:number>, <fn:boolean>, or <fn:null>

You can write standard <xsl:template> rules matching these fn:* elements to transform the resulting structure into any custom XML schema desired.


Converting XML directly to JSON with fn:xml-to-json()

The fn:xml-to-json() function performs the inverse operation, converting an XML node tree formatted according to the standard http://www.w3.org/2005/xpath-functions schema into a serialized JSON string.

Example Usage

<xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="fn">

    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:variable name="source-xml">
        <fn:map>
            <fn:string key="status">success</fn:string>
            <fn:array key="codes">
                <fn:number>200</fn:number>
                <fn:number>201</fn:number>
            </fn:array>
        </fn:map>
    </xsl:variable>

    <xsl:template name="xsl:initial-template">
        <xsl:value-of select="xml-to-json($source-xml, map{'indent': true()})"/>
    </xsl:template>
</xsl:stylesheet>

The second argument to xml-to-json() is optional and accepts an XPath map with configuration options such as indent: true() for pretty-printing.


End-to-End Bidirectional Workflow

To bridge arbitrary custom XML and JSON formats:

  1. Custom XML to JSON: Use standard XSLT templates to transform your domain-specific XML into the intermediate fn:map/fn:array structure, then pass that tree to xml-to-json().
  2. JSON to Custom XML: Parse the raw JSON using json-to-xml(), then apply templates matching the generated fn:map, fn:array, and fn:string nodes to construct your domain-specific XML structure.