问题描述
我有以下代码,它将XML作为输入,并生成许多其他文件作为输出。
public void transformXml(InputStream inputFileStream,Path outputDir) {
try {
Resource resource = resourceLoader
.getResource("classpath:demo.xslt");
LOGGER.info("Creating output XMLs and Assessment Report in {}",outputDir);
final File outputFile = new File(outputDir.toString());
final Processor processor = getSaxonProcessor();
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable stylesheet = compiler.compile(new StreamSource(resource.getFile()));
Xslt30Transformer transformer = stylesheet.load30();
Serializer out = processor.newSerializer(outputFile);
out.setoutputProperty(Serializer.Property.METHOD,"xml");
transformer.transform(new StreamSource(inputFileStream),out);
LOGGER.debug("Generated DTD XMLs and Assessment Report successfully in {}",outputDir);
} catch (SaxonApiException e) {
throw new XmlTransformationException("Error occured during transformation",e);
} catch (IOException e) {
throw new XmlTransformationException("Error occured during loading XSLT file",e);
}
}
private Processor getSaxonProcessor() {
final Configuration configuration = Configuration.newConfiguration();
configuration.disableLicensing();
Processor processor = new Processor(configuration);
return processor;
}
XML输入包含一个DOCTYPE标记,该标记解析为我无法使用的DTD。因此,为什么要使用目录将其指向类路径上的虚拟DTD。 我正在努力寻找解决方法。我在那里找到的大多数示例都没有使用s9api实现。有什么想法吗?
解决方法
代替
new StreamSource(inputFileStream)
您应该实例化一个SAXSource
,其中包含一个初始化为使用目录解析器作为其XMLReader
的{{1}}。
如果您需要对其他源文档执行相同的操作,例如使用EntityResolver
或doc()
读取的文档,则应提供document()
本身返回一个{{1} }以相同的方式初始化。
还有其他使用Saxon配置属性的方法,但是我认为以上是最简单的。