我尝试将带有org.w3c.dom.Document的Java Transformer用于输入,输出和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-File,以及如何修复它?那么它会返回正确的转换后的XML吗?
由于
答案 0 :(得分:4)
确保你
factory.setNamespaceAware(true);
使用XSLT时{p> DocumentBuilderFactory
(对于样式表和输入文档,如果您将它们创建为DOM文档;一般来说,简单地使用StreamSource
更简单,更高效如果要将XML和/或XSLT加载为文件)。