2017-07-24 172 views
1

我能够使用XSLT 3.0将JSON转换为XML。在转换为XML字段时,主题中的数据缺少XML结构。Json To Xml使用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代码:

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

我不认为''“{\ n”'编译。所以你可能想花时间准备一个最小但完整的格式化的代码示例,允许其他人重现该问题;如果用Java运行Saxon是个问题,甚至可能会出现一个新问题,第一个句子仍然是“当转换为XML字段时,主题中的数据缺少XML结构”以及如果给出的答案是奇怪的,那么编辑问题以询问一个新问题 –

+1

你正在做一个可怕的工作来格式化你的代码片段我已经编辑了一些适用于我的Saxon的Java代码的答案9.8 HE。 –

+0

@MartinHonnen谢谢,我可以使用同一段Java代码从XML转换为JSON。 – bookofcodes

回答

2

添加一件T 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> 

确保将字符串数据解析为XML。

请注意,使用XSLT 3.0,您可以使用<xsl:mode on-no-match="shallow-copy"/>而不必拼写出标识转换。

一个简单的例子是

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

其(当与撒克逊运行9.8.0.3 HE与it:init)输出

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

在这里是基于一个完整的Java程序在您的代码片段张贴在各种编辑:

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

} 

当编译和运行对撒克逊9.8.0.3 HE我得到输出

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

并且没有错误。样式表如上图所示,没有预设的参数内容:

+0

它会在投射时抛出错误。 – bookofcodes

+0

@bookofcodes,你可以编辑你的问题,并显示一个最小但完整的代码示例以及用于运行代码的软件的确切错误信息和准确信息吗? –

+0

我编辑了这个问题,我正在尝试使用上面的代码使用java。 – bookofcodes