I am able to transform JSON to XML using XSLT 3.0. When transforming to XML fields the data in subjects are missing the XML structure.
{
"student" : "john",
"class" : "Bachelors",
"subjects" : "<college><subject><subjects>maths</subjects><term>spring</term></subject></college>"
}
XSLT :
<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:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="init">
<xsl:apply-templates select="json-to-xml($jsonText)"/>
</xsl:template>
</xsl:stylesheet>`
Java Code :
public static void main(String[] args){
final String XSLT_PATH = "src/so/test1/json2xml.xsl";
final String JSON = ""{\n" +" \"student\": \"john\",\n" +
" \"class\": \"Bachelors\"\n" +
" \"subjects\": \"<college><subject><subjects>maths</subjects><term>spring</term></subject></college>"\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"));
transformer.setParameter(new QName("jsonText"), new
XdmAtomicValue(JSON));
transformer.setDestination(serializer);
transformer.transform();
}
Error:
Error at char 12 in xsl:apply-templates/@select on line 8 column 58 of json2xml.xsl:
FOJS0001: Invalid JSON input: Unescaped control character (xd)
Exception in thread "main" net.sf.saxon.s9api.SaxonApiException: Invalid JSON input: Unescaped control character (xd)
at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:599)
at com.xmltojson.sampleclass.SimpleJaxp.main(SimpleJaxp.java:49)
Caused by: net.sf.saxon.trans.XPathException: Invalid JSON input: Unescaped control character (xd)
""{\n"compiles. So you might want to take your time to prepare a minimal but complete and well-formatted code sample that allows others to reproduce the problem. And perhaps even start a new question if running Saxon with Java is the problem, the first sentence still reads "When transforming to XML fields the data in subjects are missing the XML structure" and if an answer to that is given it is rather strange to edit the question to ask about a new problem.<xsl:output method="json"/>or<xsl:output method="adaptive"/>in the XSLT should suffice if you your stylesheet returns a map or an array.