我有下面的代码,它以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实现。有什么想法吗?
发布于 2020-10-20 08:38:36
而不是
new StreamSource(inputFileStream)您应该实例化一个SAXSource,其中包含一个初始化为使用目录解析器作为其EntityResolver的XMLReader。
如果您需要对其他源文档(例如使用doc()或document()读取的文档)执行相同的操作,则应该提供一个URIResolver,它本身返回以相同方式初始化的SAXSource。
还有其他使用Saxon配置属性的方法,但我认为上面的方法是最简单的。
https://stackoverflow.com/questions/64440936
复制相似问题