首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中向XSLT Saxon s9api添加目录

在Java中向XSLT Saxon s9api添加目录
EN

Stack Overflow用户
提问于 2020-10-20 08:03:18
回答 1查看 118关注 0票数 0

我有下面的代码,它以XML作为输入,并生成一堆其他文件作为输出。

代码语言:javascript
复制
   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实现。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-20 08:38:36

而不是

代码语言:javascript
复制
new StreamSource(inputFileStream)

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

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64440936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档