jcoder
jcoder

Reputation: 23

unable to transform from json to xml with xslt in java

I am trying to transform JSON to XML using XSLT 3.0 in Java. When i run the Java program, it displays an error: Content is not allowed in prolog.

{
    color: "red",
    value: "#f00"
}

Upvotes: 1

Views: 2120

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

Based on that error it sounds like you're using the JSON as the input to the stylesheet. Like mentioned in the other answer, XSLT will only work on XML input.

However, you can run a stylesheet without any input by using an initial template. You can then pass in the path to the JSON file through an xsl:param and read it with unparsed-text(). This will give you a string you can convert to XML with json-to-xml(). (You could also pass the JSON itself in as an xsl:param and use that param directly in the json-to-xml() call.)

Also, in your sample JSON, the keys need to be quoted.

Here's an example run from the command line using Saxon-HE 9.8...

JSON (so.json)

{
    "color": "red",
    "value": "#f00"
}

XSLT 3.0 (json2xml.xsl)

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="jsonFile"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template name="init">
    <xsl:variable name="json" select="unparsed-text($jsonFile)"/>
    <xsl:apply-templates select="json-to-xml($json)"/>
  </xsl:template>

</xsl:stylesheet>

Command Line/Output

C:\temp>java -cp "C:/apps/saxon/saxon9he.jar" net.sf.saxon.Transform -it:"init" -xsl:"json2xml.xsl" jsonFile="so.json"
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="color">red</string>
   <string key="value">#f00</string>
</map>

Here's a Java example (using Saxon-HE 9.7) showing how to pass the JSON in as a parameter...

Java

package so.test1;

import java.io.File;
import java.io.OutputStream;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.s9api.XsltTransformer;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;

/**
 *
 * @author dhaley
 *
 */
public class SOTest1 {

    public static void main(String[] args) throws SaxonApiException {
        final String XSLT_PATH = "src/so/test1/json2xml.xsl";
        final String JSON = "{\n" +
                            "    \"color\": \"red\",\n" +
                            "    \"value\": \"#f00\"\n" +
                            "}";

        OutputStream outputStream = System.out;        
        Processor processor = new Processor(false);        
        Serializer serializer = processor.newSerializer();
        serializer.setOutputStream(outputStream);        
        XsltCompiler compiler = processor.newXsltCompiler();
        XsltExecutable executable = compiler.compile(new StreamSource(new File(XSLT_PATH)));        
        XsltTransformer transformer = executable.load();
        transformer.setInitialTemplate(new QName("init")); //<-- SET INITIAL TEMPLATE
        transformer.setParameter(new QName("jsonText"), new XdmAtomicValue(JSON)); //<-- PASS JSON IN AS PARAM
        transformer.setDestination(serializer);
        transformer.transform();

    }

}

XSLT 3.0 (updated json2xml.xsl)

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="jsonText"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template name="init">
    <xsl:apply-templates select="json-to-xml($jsonText)"/>
  </xsl:template>

</xsl:stylesheet>

Output (System.out)

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="color">red</string>
   <string key="value">#f00</string>
</map>

Upvotes: 6

Jason
Jason

Reputation: 11832

XSLT works on XML input, not JSON.

A simple Google search found this code on GitHub which claims to be able to convert JSON to XML and vice-versa.

Upvotes: 1

Related Questions