【发布时间】:2020-06-18 14:38:28
【问题描述】:
我想知道这一点,这样我就可以将 xsl 转换应用于 xml 文档而不会丢失一些实体,例如 –
如何告诉解析器(我不关心的任何解析器)使用哪个目录,然后执行 xsl 转换?,如何将新配置的解析器连接到转换工厂。
下面的代码代表我想对 xml 文件执行的转换(它工作正常)。我只想知道如何添加 XML 目录方法,以便 xml 文档正确加载其 DTD 并继续执行 xsl 转换步骤。
try {
SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
Templates step1Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step1.xsl")
));
Templates step2Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step2.xsl")
));
Templates step3Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step3.xsl")
));
TransformerHandler th1 = stf.newTransformerHandler(step1Template);
TransformerHandler th2 = stf.newTransformerHandler(step2Template);
TransformerHandler th3 = stf.newTransformerHandler(step3Template);
StreamSource xmlStreamSource = new StreamSource(new File(xmlInputFile));
StreamResult outputStreamSource1 = new StreamResult(new File (outputNewFile1));
StreamResult outputStreamSource2 = new StreamResult(new File (outputNewFile2));
th1.setResult(new SAXResult(th2));
th2.setResult(new SAXResult(th3));
th3.setResult(outputStreamSource1);
Transformer t = stf.newTransformer();
t.transform(xmlStreamSource, new SAXResult(th1));
}catch (TransformerException e){
e.printStackTrace();
return false;
}
这是一个包含实体的 xmlInputFile 示例
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE manual PUBLIC '-//docufy//Docufy Standard DTD 20080125//EN' '/system/cosimago/dtd/manual.dtd'>
<chapter>
<title>LEDs „5 – 8“ am CPU-Board prüfen</title>
<body>
<!-- just content -->
</body>
</chapter>
如果有好心人帮助我解决这个问题,我将不胜感激。
提前谢谢你。
安德烈斯
【问题讨论】:
-
那么你在哪里有像
&ndash;这样的实体引用?在原始 XML 中输入xmlInputFile?如果您准确地展示您的输入以及您获得的结果以及您期望的结果,将会有所帮助。 -
@MartinHonnen 您好 Martin,是的,实体引用在 XmlInputFile 中。我添加了一个输入示例。我还没有得到任何结果,因为 xml 文档没有加载它的 DTD。我想要的结果是它加载 DTD、解析实体并最终允许在其上执行 xsl 转换
-
XML 目录是一种不在网络上加载 DTD 的方法,在这些 HMTL 实体的情况下大大加快了速度。它是如此重要,您最好对其进行研究,而不是选择一个“有效”的答案。