在 Java 中向 XSLT Saxon s9api 添加目录
Adding a catalog to XSLT Saxon s9api in Java
我有以下代码,它将 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
初始化为使用目录解析器作为它的 EntityResolver
。
如果您需要对其他源文档执行相同的操作,例如使用 doc()
或 document()
阅读的文档,您应该提供 URIResolver
本身 returns a SAXSource
以同样的方式初始化。
还有其他使用 Saxon 配置属性的方法,但我认为上面的方法是最简单的。
我有以下代码,它将 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
初始化为使用目录解析器作为它的 EntityResolver
。
如果您需要对其他源文档执行相同的操作,例如使用 doc()
或 document()
阅读的文档,您应该提供 URIResolver
本身 returns a SAXSource
以同样的方式初始化。
还有其他使用 Saxon 配置属性的方法,但我认为上面的方法是最简单的。