XML technologies / JSON for XML professionals / JSON with XSLT: processing
Setting JSON against XSLT would be misleading. XSLT is not a technology restricted to XML output. With a suitable XSLT 3.0 implementation and the required map XPath, Map, array and JSON functions, an existing transformation environment can also read, process and output JSON. An XSLT 1.0 processor does not offer these functions. Exact support must be checked for the product and version used. [Q06] [Q26]
This is strategically important for XML teams: JSON as a new interface format does not force a complete switch to JavaScript. Extending an existing transformation step with JSON input or output may suffice.
A familiar XML starting point
First, the same catalog selection in a deliberately simple XSLT 1.0 stylesheet. This version reads the XML catalog and produces an HTML list.
File: examples/xslt/catalog-html-1.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/catalog">
<ul>
<xsl:for-each select="book[@available = 'true']">
<xsl:sort select="@priceCents" data-type="number"/>
<li><xsl:value-of select="title"/></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
This stylesheet was executed using the lxml/libxslt runtime documented in the package. The result shows the same available books in the same price order as the Python, JavaScript and jq examples.
File: results/core/catalog-from-xml.html
<ul>
<li>data2type Publishing APIs (Demo)</li>
<li>Schematron</li>
</ul>
The comparison therefore does not depend on differently structured examples. The domain task stays identical; only the input model and processing access change.
Reading JSON directly as maps and arrays
json-doc reads a JSON resource. parse-json processes existing JSON text. In the XPath/XDM model, JSON objects are represented as maps and JSON arrays as arrays. The lookup operator ? provides access to entries and array members. [Q26]
File: examples/xslt/json-to-html.xsl
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
<xsl:output method="html" html-version="5" indent="yes"/>
<xsl:param name="input-uri" as="xs:string" required="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:variable name="data" select="json-doc($input-uri)"/>
<ul>
<xsl:for-each select="$data?books?*[?available = true()]">
<xsl:sort select="?priceCents" data-type="number"/>
<li><xsl:value-of select="?title"/></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>The expression $data?books?* returns the catalog array’s members. The following predicate expression selects available titles. xsl:sort and xsl:value-of remain familiar tools. Unlike with /catalog/book however, we navigate maps and values rather than element nodes.
Because the input here is not an XML document, the stylesheet starts with an initial template and a URI parameter. The test environment supplies an absolute file URI. A path relative to the current terminal directory is not automatically identical to a URI relative to a stylesheet’s base URI.
Expected HTML structure, regardless of indentation and serializer details:
File: expected/catalog-list.html
<ul><li>data2type Publishing APIs (Demo)</li><li>Schematron</li></ul>The XSLT 3.0 example is prepared in the package with a separate runtime test. Successfully parsing the .xsl file as XML explicitly does not count as executing this stylesheet. The test report distinguishes these two checks.
Mapping XML to domain-specific JSON
The reverse direction requires a modeling decision. The following stylesheet reads the XML catalog and creates a map with an array of book maps. Prices are explicitly converted to integers; the attribute available is converted to a boolean.
File: examples/xslt/xml-to-json.xsl
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
<xsl:output method="json" indent="yes"/>
<xsl:template match="/catalog">
<xsl:sequence select="map {
'books': array {
for $book in book return map {
'id': string($book/@id),
'title': string($book/title),
'available': xs:boolean($book/@available),
'priceCents': xs:integer($book/@priceCents)
}
}
}"/>
</xsl:template>
</xsl:stylesheet>xsl:output method="json" serializes the constructed data structure as JSON. JSON output is therefore not string concatenation with manually inserted commas. This prevents many syntax errors. The domain-specific selection of properties nevertheless remains the stylesheet’s responsibility. [Q27]
The example maps a catalog; it is not a universal XML-to-JSON converter. This specific selection would not automatically preserve comments, processing instructions, document type declarations or arbitrary mixed content. The restriction is not an error if precisely this API view is intended and documented.
JSON as a standardized XML representation
In addition to maps and arrays, there is json-to-xml. The function maps JSON values to XML elements in the namespace http://www.w3.org/2005/xpath-functions. The result type describes JSON structures using elements such as map, array, string, number, boolean and null. A property title therefore does not automatically become a domain-specific element <title>. [Q26]
An abbreviated illustration of this representation uses the separate null-value exercise. It is neither a complete book description nor the output for the catalog:
File: expected/w3c-xml-example.xml
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="title">Schematron</string>
<boolean key="available">true</boolean>
<null key="subtitle"/>
</map>The following stylesheet produces the corresponding complete representation of its JSON input:
File: examples/xslt/json-to-w3c-xml.xsl
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="input-uri" as="xs:string" required="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:sequence select="json-to-xml(unparsed-text($input-uri))"/>
</xsl:template>
</xsl:stylesheet>This bridge is particularly useful when a team wants to reuse existing template patterns and node operations. It transforms an XML tree whose vocabulary represents JSON types. This tree is not yet a domain-specific XML structure such as JATS or DocBook.
Template rules on the XML bridge
The next stylesheet combines json-to-xml with xsl:apply-templates. The prefix j is bound to the function namespace. Key names are stored in @key. Access is therefore slightly more verbose than a map lookup, but processing again follows the familiar template principle.
File: examples/xslt/json-template-rules.xsl
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:j="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="#all">
<xsl:output method="html"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:param name="input-uri" as="xs:string" required="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:variable name="json-xml" select="json-to-xml(unparsed-text($input-uri))"/>
<ul>
<xsl:apply-templates select="$json-xml/j:map/j:array[@key='books']/j:map[j:boolean[@key='available']='true']">
<xsl:sort select="j:number[@key='priceCents']" data-type="number"/>
</xsl:apply-templates>
</ul>
</xsl:template>
<xsl:template match="j:map">
<li><xsl:value-of select="j:string[@key='title']"/></li>
</xsl:template>
</xsl:stylesheet>Both approaches solve the same task: direct processing of maps and arrays or transformation of an XML representation. The choice should depend on existing libraries, team expertise, node-operation requirements and target-model complexity, not a blanket prohibition of the other approach.
The return path and double serialization
xml-to-json expects the defined XML representation of JSON. It is not a function that interprets any arbitrary <book> document according to a domain model. The following stylesheet demonstrates the syntactic round trip within this representation.
File: examples/xslt/w3c-xml-roundtrip.xsl
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
<xsl:output method="text"/>
<xsl:param name="input-uri" as="xs:string" required="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:sequence select="xml-to-json(json-to-xml(unparsed-text($input-uri)))"/>
</xsl:template>
</xsl:stylesheet>The return value of xml-to-json is already JSON text in a string. This example therefore uses text output. Serializing the same string again with the JSON output method would produce a JSON string with escaped quotation marks rather than the intended JSON object. Structural values and already serialized text must be kept distinct. [Q26] [Q27]
The same applies to serialize(..., map { 'method': 'json' }): explicitly invoking serialization returns text. Passing a map or array to a JSON serializer, on the other hand, supplies a data structure. This distinction prevents a common class of errors at pipeline boundaries.
