在Java中将目录添加到XSLT Saxon s9api

问题描述

我有以下代码,它将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}}。

如果您需要对其他源文档执行相同的操作,例如使用EntityResolverdoc()读取的文档,则应提供document()本身返回一个{{1} }以相同的方式初始化。

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...