Transforming JSON to XML with XSLT 3: A Practical Guide
Problem: You have a JSON file produced by an external system, but you need to work with its data in an XML format.
Rephrased: Imagine receiving data from an external source in a JSON format, but your systems only understand XML. How do you seamlessly convert this data to be compatible with your existing infrastructure?
Solution: XSLT 3.0, the latest iteration of the Extensible Stylesheet Language for Transformations, provides a powerful and elegant solution for this task. It allows you to transform JSON data into a well-formed XML structure with ease.
Scenario: Let's say you have a JSON file named "products.json" containing the following structure:
[
{
"id": 1,
"name": "Laptop",
"price": 1200,
"category": "Electronics"
},
{
"id": 2,
"name": "Book",
"price": 20,
"category": "Literature"
}
]
We want to transform this JSON into a corresponding XML structure:
<products>
<product>
<id>1</id>
<name>Laptop</name>
<price>1200</price>
<category>Electronics</category>
</product>
<product>
<id>2</id>
<name>Book</name>
<price>20</price>
<category>Literature</category>
</product>
</products>
XSLT 3 Code: Here's the XSLT 3 stylesheet that accomplishes this transformation:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:json="http://www.w3.org/2005/xpath-functions/json">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<products>
<xsl:for-each select="json:parse(unparsed-text('products.json'))">
<product>
<xsl:for-each select="*">
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</product>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>
Explanation:
json:parse()
function: This function takes the content of the JSON file ("products.json") and parses it into an in-memory JSON document.xsl:for-each
for arrays: We loop through the top-level array of JSON objects usingfor-each
.xsl:for-each
for object properties: For each object, we loop through its properties using anotherfor-each
.local-name()
: This function retrieves the local name of each property (e.g., "id," "name," etc.) and uses it to create the corresponding XML element.xsl:value-of
: The value of each property is assigned to the newly created XML element.
Benefits of XSLT 3 for JSON to XML Transformation:
- Powerful JSON Processing: XSLT 3.0 provides the
json
namespace with functions likejson:parse
,json:object
, andjson:array
for manipulating JSON data seamlessly. - Maintainability: XSLT is declarative and uses a structured syntax, making the transformations easier to understand and maintain.
- Extensibility: XSLT offers a wide range of functions and features for advanced transformation needs, including error handling, custom formatting, and dynamic generation of XML structures.
Additional Value:
- Error Handling: Include error handling in your XSLT stylesheet to gracefully manage unexpected JSON structures or invalid data.
- Customization: Tailor your transformation to match your specific XML requirements, including element names, attributes, and namespaces.
- Integration: Use XSLT in your existing pipelines or scripting environments to seamlessly integrate JSON data into your XML workflows.
Resources:
- XSLT 3.0 Specification: https://www.w3.org/TR/xslt-30/
- XSLT 3.0 Tutorial: https://www.w3.org/TR/xslt-30/
Conclusion: XSLT 3.0 provides a reliable and flexible solution for transforming JSON files to XML. Its powerful features and declarative syntax streamline the conversion process, making it a valuable tool for managing data in diverse formats.