[英]Adding a catalog to XSLT Saxon s9api in Java
I have the following code which takes XML as input and produces a bunch of other files as output.我有以下代码,它将 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;
}
The XML input contains a DOCTYPE tag which resolves to a DTD that is not available to me. XML 输入包含一个 DOCTYPE 标记,该标记解析为我不可用的 DTD。 Hence why I am wanting to use a catalog to point it to a dummy DTD which is on my classpath.因此,为什么我想使用目录将其指向我的类路径上的虚拟 DTD。 I am struggling to find a way to this.我正在努力寻找解决这个问题的方法。 Most examples that I find out there, are not using the s9api implementation.我在那里找到的大多数示例都没有使用 s9api 实现。 Any ideas?有任何想法吗?
Instead of代替
new StreamSource(inputFileStream)
you should instantiate a SAXSource
, containing an XMLReader
initialized to use the catalog resolver as its EntityResolver
.您应该实例化一个SAXSource
,其中包含一个初始化为使用目录解析器作为其EntityResolver
的XMLReader
。
If you need to do the same thing for other source documents, such as those read using doc()
or document()
, you should supply a URIResolver
which itself returns a SAXSource
initialized in the same way.如果您需要对其他源文档执行相同的操作,例如使用doc()
或document()
读取的那些,您应该提供一个URIResolver
,它本身返回一个以相同方式初始化的SAXSource
。
There are other ways of doing it using Saxon configuration properties, but I think the above is the simplest.还有其他使用 Saxon 配置属性的方法,但我认为以上是最简单的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.