2017-07-24 4 views
1

Ich bin in der Lage, JSON zu XML mit XSLT 3.0 zu transformieren. Bei der Transformation in XML-Felder fehlt den Daten in Subjekten die XML-Struktur.Json zu Xml mit xslt

{ 
    "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(); 
} 

Fehler:

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) 
+0

Ich glaube nicht, '„“{\ n" 'kompiliert, damit Sie Ihre Zeit in Anspruch nehmen möchten eine minimale vorzubereiten, aber vollständige und gut. -formatiertes Codebeispiel, das es anderen ermöglicht, das Problem zu reproduzieren und vielleicht sogar eine neue Frage zu starten, wenn Saxon mit Java ausgeführt wird, lautet der erste Satz immer noch "Bei der Transformation in XML-Felder fehlt den Daten in Subjekten die XML-Struktur" und wenn eine Antwort darauf gegeben ist, ist es ziemlich seltsam, die Frage zu bearbeiten, um nach einem neuen Problem zu fragen. " –

+1

Sie machen einen schrecklichen Job beim Formatieren Ihrer Code-Schnipsel. Ich habe meine Antwort mit etwas Java-Code bearbeitet, der für mich mit Saxon funktioniert 9.8 HE. –

+0

@MartinHonnen Danke, kann ich das gleiche Stück Java-Code verwenden Umwandlung von XML in JSON. – bookofcodes

Antwort

2

ein T hinzufügen emplate

<xsl:template match="string[@key = 'subjects']" xpath-default-namespace="http://www.w3.org/2005/xpath-functions"> 
    <xsl:copy> 
    <xsl:copy-of select="@*"/> 
    <xsl:sequence select="parse-xml(.)/node()"/> 
    </xsl:copy> 
</xsl:template> 

, um sicherzustellen, dass die Zeichenfolgedaten in XML analysiert werden.

Beachten Sie, dass Sie mit XSLT 3.0 <xsl:mode on-no-match="shallow-copy"/> verwenden können, anstatt die Identitätstransformation buchstabieren zu müssen.

Ein kurzes Beispiel ist

<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"><![CDATA[{ 
    "student" : "john", 
    "class" : "Bachelors", 
    "subjects" : "<college><subject><subjects>maths</subjects><term>spring</term></subject></college>" 
    }]]></xsl:param> 
    <xsl:mode on-no-match="shallow-copy"/> 
    <xsl:template name="init"> 
     <xsl:apply-templates select="json-to-xml($jsonText)"/> 
    </xsl:template> 

    <xsl:template match="string[@key = 'subjects']" xpath-default-namespace="http://www.w3.org/2005/xpath-functions"> 
    <xsl:copy> 
    <xsl:copy-of select="@*"/> 
    <xsl:sequence select="parse-xml(.)/node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

, die (wenn sie mit Saxon 9.8.0.3 HE mit it:init laufen) gibt

<?xml version="1.0" encoding="UTF-8"?> 
<map xmlns="http://www.w3.org/2005/xpath-functions"> 
    <string key="student">john</string> 
    <string key="class">Bachelors</string> 
    <string key="subjects"> 
     <college xmlns=""> 
     <subject> 
      <subjects>maths</subjects> 
      <term>spring</term> 
     </subject> 
     </college> 
    </string> 
</map> 

ist hier ein Programm vollständig Java basiert auf der Code-Schnipsel in verschiedenen geschrieben Bearbeitungen:

import java.io.File; 
import java.io.OutputStream; 
import javax.xml.transform.stream.StreamSource; 
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; 
import net.sf.saxon.s9api.XsltTransformer; 

public class Saxon98HETest1 { 

    public static void main(String[] args) throws SaxonApiException { 
     final String XSLT_PATH = "sheet1.xsl"; 
     String JSON = "{\n" + 
" \"student\" : \"john\",\n" + 
" \"class\" : \"Bachelors\",\n" + 
" \"subjects\" : \"<college><subject><subjects>maths</subjects><term>spring</term></subject></college>\"\n" + 
"}"; 
     testJsonToXml(XSLT_PATH, JSON); 
     System.out.println(); 

     JSON = "{\n" + 
          " \"color\": \"red\",\n" + 
          " \"value\": \"#f00\"\n" + 
          "}"; 
     testJsonToXml(XSLT_PATH, JSON); 
     System.out.println(); 
    } 

    static void testJsonToXml(String xsl, String json) throws SaxonApiException { 
     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(xsl))); 
     XsltTransformer transformer = executable.load(); 
     transformer.setInitialTemplate(new QName("init")); 
     transformer.setParameter(new QName("jsonText"), new XdmAtomicValue(json)); 
     transformer.setDestination(serializer); 
     transformer.transform();  
    } 

} 

Wenn kompiliert und ausgeführt gegen Saxon 9.8.0.3 HE I Holen Sie sich die Ausgabe

<?xml version="1.0" encoding="UTF-8"?> 
<map xmlns="http://www.w3.org/2005/xpath-functions"> 
    <string key="student">john</string> 
    <string key="class">Bachelors</string> 
    <string key="subjects"> 
     <college xmlns=""> 
     <subject> 
      <subjects>maths</subjects> 
      <term>spring</term> 
     </subject> 
     </college> 
    </string> 
</map> 

<?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> 

und keine Fehler. Das Stylesheet ist, wie oben gezeigt, ohne den voreingestellten param Inhalt:

<?xml version="1.0" encoding="UTF-8"?> 
<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:param> 
    <xsl:mode on-no-match="shallow-copy"/> 
    <xsl:template name="init"> 
     <xsl:apply-templates select="json-to-xml($jsonText)"/> 
    </xsl:template> 

    <xsl:template match="string[@key = 'subjects']" xpath-default-namespace="http://www.w3.org/2005/xpath-functions"> 
    <xsl:copy> 
    <xsl:copy-of select="@*"/> 
    <xsl:sequence select="parse-xml(.)/node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

es wirft Fehler beim Casting. – bookofcodes

+0

@bookofcodes, können Sie Ihre Frage bearbeiten und ein minimales, aber vollständiges Codebeispiel zusammen mit der genauen Fehlermeldung und genauen Informationen der Software anzeigen, mit der Sie den Code ausführen? –

+0

Ich habe die Frage bearbeitet Ich versuche, den obigen Code mit Java zu verwenden. – bookofcodes