In XSLT, it is recommended that you specify the intended media type in the output properties of your XSLT with the <xsl:output> element as:

<xsl:output method="xml" media-type="application/json"/>

This explicitly says that the stylesheet produces XML but intends to generate JSON data.


In Java, you can directly specify the Result implementation to use as JSON result.

TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("stylesheet.xsl"));
Transformer transformer = factory.newTransformer(xslt);
Source source = new StreamSource(new File("source.xml"));
JSONResult result = new JSONResult(System.out);
transformer.transform(source, result);

Or if you already have StreamResult, you can simply build a new one after checking the output properties of the Transformer:

StreamResult result = new StreamResult(new File("result.json"));
// Check that XSLT specifies method "xml" and media type "application/json"
if (JSONResult.supports(transformer))  {
  Result jsonresult = JSONResult.newInstance(result);
  transformer.transform(source, jsonresult);
} else {
  transformer.transform(source, result);



From version 0.9.21, Berlioz has built-in support for Aeson, simply include the JSON processing API and set the output properties of your XSLT as described above and Berlioz should automatically produce JSON.

Created on , last edited on