为XSLT Saxon s9api在Java中添加目录。

huangapple go评论80阅读模式
英文:

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 occurred during transformation", e);
  } catch (IOException e) {
    throw new XmlTransformationException("Error occurred 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实现。有什么想法吗?

英文:

I have the following code which takes XML as input and produces a bunch of other files as output.

   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. Hence why I am wanting to use a catalog to point it to a dummy DTD which is on my classpath.
I am struggling to find a way to this. Most examples that I find out there, are not using the s9api implementation. Any ideas?

答案1

得分: 1

代替使用:

new StreamSource(inputFileStream)

您应该实例化一个SAXSource,其中包含一个已初始化为使用目录解析器作为其EntityResolverXMLReader

如果您需要对其他源文档执行相同的操作,例如使用doc()document()读取的文档,您应该提供一个URIResolver,该解析器本身返回以相同方式初始化的SAXSource

还有其他使用Saxon配置属性的方法,但我认为上述方法是最简单的。

英文:

Instead of

new StreamSource(inputFileStream)

you should instantiate a SAXSource, containing an XMLReader initialized to use the catalog resolver as its EntityResolver.

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.

There are other ways of doing it using Saxon configuration properties, but I think the above is the simplest.

huangapple
  • 本文由 发表于 2020年10月20日 16:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64440936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定