XSLT 3.0 and XPath 3.1 introduced powerful features for processing JSON alongside traditional XML. These standards enable developers to parse, transform, and output JSON data using familiar XSLT and XPath techniques. This tutorial explores their JSON capabilities with practical examples.
Why JSON in XSLT and XPath?
XSLT was built for XML, but XPath 3.1 adds native JSON parsing and querying, so an XSLT stylesheet can consume JSON directly. That lets you apply XSLT's declarative transformation model to the JSON that APIs and web applications produce, without a separate conversion step — and handle mixed XML and JSON input in one pipeline.
Key Features
- JSON to XML: XSLT 3.0 can read JSON and turn it into XML. You can then process it with familiar XSLT tools.
- JSON functions: XPath 3.1 has functions like
parse-json()andjson-to-xml(). These let you work with JSON directly. - XML to JSON: When you are done, use
xml-to-json()to write your data back out as JSON. - Maps and arrays: XPath 3.1 adds maps and arrays. These match the way JSON stores data.
Getting Started
To follow along, you'll need an XSLT 3.0 processor like Saxon-HE. Most modern processors support XPath 3.1 by default.
Sample Input
Save this as data.json:
{
"users": [
{"id": 1, "name": "Alice", "active": true},
{"id": 2, "name": "Bob", "active": false}
]
}
The goal: produce a simplified output listing only the active users' names.
Parsing JSON with XPath 3.1
XPath 3.1 gives you two ways to handle JSON. The parse-json() function turns a JSON string into an XPath map or array:
parse-json('{"name": "Alice"}')
You can also treat JSON as XML. For that, use json-to-xml(). It produces an XML version of the data. You can then query it with standard XPath or transform it with XSLT:
json-to-xml(unparsed-text('data.json'))
This function reads data.json and returns an XML tree. The tree lives in the http://www.w3.org/2005/xpath-functions namespace. Objects become <map> elements. Arrays become <array> elements. Each value carries its type as the element name:
<map xmlns="http://www.w3.org/2005/xpath-functions">
<array key="users">
<map>
<number key="id">1</number>
<string key="name">Alice</string>
<boolean key="active">true</boolean>
</map>
<map>
<number key="id">2</number>
<string key="name">Bob</string>
<boolean key="active">false</boolean>
</map>
</array>
</map>
Transforming JSON with XSLT 3.0
Below is an XSLT stylesheet (transform.xsl) that filters active users and outputs their names as JSON:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="3.0">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template name="xsl:initial-template">
<xsl:variable name="json-input" select="json-to-xml(unparsed-text('data.json'))"/>
<xsl:variable name="active-users">
<fn:array>
<xsl:for-each select="$json-input/fn:map/fn:array[@key='users']/fn:map[fn:boolean[@key='active']='true']">
<fn:map>
<fn:string key="name"><xsl:value-of select="fn:string[@key='name']"/></fn:string>
</fn:map>
</xsl:for-each>
</fn:array>
</xsl:variable>
<xsl:value-of select="xml-to-json($active-users, map{'indent':true()})"/>
</xsl:template>
</xsl:stylesheet>
How It Works
unparsed-text('data.json')reads the file. It turns the JSON into a plain string.json-to-xml()converts that string into XML.xsl:for-eachpicks the maps whereactiveistrue. It builds a new array of maps. Each map keeps only thenamefield.xml-to-json()turns the result back into JSON. Indentation is on, so the output is easy to read.
Run this with Saxon:
java -jar saxon-he-10.8.jar -xsl:transform.xsl -it
Output:
[
{
"name": "Alice"
}
]
XPath 3.1 Map and Array Functions
XPath 3.1 treats JSON data as maps and arrays, with functions and operators to match. map:get($map, 'key') retrieves a value from a map, and the map constructor map{'key': 'value'} builds one. For arrays, $array?1 gets the first item, and the lookup operator doubles as a filter: $array?*[?active=true()] returns only the items whose active flag is true. Since maps and arrays mirror JSON objects and arrays one-to-one, queries over JSON read naturally.
Advanced Example: Nested JSON
For nested JSON like:
{
"departments": [
{
"name": "Engineering",
"employees": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
}
]
}
To extract employee names per department, use:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="3.0">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template name="xsl:initial-template">
<xsl:variable name="json-input" select="json-to-xml(unparsed-text('nested.json'))"/>
<xsl:variable name="result">
<fn:map>
<xsl:for-each select="$json-input/fn:map/fn:array[@key='departments']/fn:map">
<fn:array key="{fn:string[@key='name']}">
<xsl:for-each select="fn:array[@key='employees']/fn:map">
<fn:string><xsl:value-of select="fn:string[@key='name']"/></fn:string>
</xsl:for-each>
</fn:array>
</xsl:for-each>
</fn:map>
</xsl:variable>
<xsl:value-of select="xml-to-json($result, map{'indent':true()})"/>
</xsl:template>
</xsl:stylesheet>
Output:
{
"Engineering": [
"Alice",
"Bob"
]
}
Tips for Success
Start small. Test your stylesheet on tiny JSON files before scaling up, and validate the input first to avoid parsing errors. For readable output, pass map{'indent':true()} to xml-to-json(). And remember XPath's ? operator — it makes map and array access concise.
Limitations
- JSON parsing is strict; invalid JSON will fail.
- Large JSON datasets may impact performance.
- Some XSLT processors may not fully support all XPath 3.1 features.
Conclusion
XSLT 3.0 and XPath 3.1 make JSON transformation accessible and powerful. By converting JSON to XML, processing it with XSLT, and outputting JSON, developers can handle modern data formats without leaving the XSLT ecosystem. Experiment with these tools to unlock new data transformation possibilities.
For further reading, check the XSLT 3.0 spec and XPath 3.1 spec.