JAVA 转换 returns XSLT 而不是转换结果

JAVA Transformation returns XSLT instead of transformation result

我尝试将 Java Transformer 与 org.w3c.dom.Document 一起用于输入、输出和 XSLT。不幸的是,输出等于 XSLT 输入。所以看起来转换没有正确使用。我无法找到此行为的任何原因,所以我在这里询问并希望得到帮助。

我的代码:

import java.io.BufferedReader;
import java.io.FileReader;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLTransform {
    public static void main(String[] args) {

        StringWriter stringWriter = null;
        try {
            // Read Input Files
            Document xslt = parseXML(readFile("...")); // link to XSLT
            Document input = parseXML(readFile("...")); // link to input

            // Create new Transformer with the XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new DOMSource(xslt));

            // create variables for the output
            stringWriter = new StringWriter();
            StreamResult streamResult = new StreamResult(stringWriter);

            // transform
            transformer.transform(new DOMSource(input), streamResult);

            // parse it to XML DOM File
            Document resultXML = parseXML(stringWriter.toString());

            // output, so we see it is wrong
            System.out.println(convertDocumentToString(resultXML));
        } catch (Exception e) {
        } finally {
            try {
                if (stringWriter != null) stringWriter.close();
            } catch (Exception e1) { };
        }
    }

    //////////////////////////////////////
    //Helper-Methods which seems to work//
    //////////////////////////////////////

    public static String convertDocumentToString(Document doc) throws TransformerFactoryConfigurationError,
            TransformerException {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        Writer out = new StringWriter();
        tf.transform(new DOMSource(doc), new StreamResult(out));
        return out.toString();
    }

    private static Document parseXML(String str) throws ParserConfigurationException, SAXException, IOException,
            SQLException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new InputSource(new StringReader(str)));
    }

    private static String readFile(String filename) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();
        }
    }
}

我使用以下输入文件:

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
    <xsl:apply-templates select="/Test/Content"/>
    </xsl:template>

    <xsl:template match="/Test/Content">
            <xsl:copy-of select="." />
    </xsl:template>
</xsl:stylesheet>

输入文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Test>
  <Content>
    <X>x</X>
    <Y>y</Y>
    <Z>z</Z>
  </Content>
  <Template>
    <Definition>
      <A>A</A>
      <B>B</B>
      <C>C</C>
    </Definition>
  </Template>
</Test>

然后我得到了奇怪的输出(等于 XSLT)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/>
<xsl:template match="/">
<xsl:apply-templates select="/Test/Content"/>
</xsl:template>
<xsl:template match="/Test/Content">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

有人知道为什么输出等于 XSLT 文件吗?如何解决?以便 returns 正确转换为 XML?

谢谢

确保你做到了

factory.setNamespaceAware(true);

DocumentBuilderFactory 使用 XSLT 时(对于样式表和输入文档,如果您将它们创建为 DOM 文档;一般来说,简单地使用它更容易和更有效a StreamSource 如果您想将 XML and/or XSLT 作为文件加载)。