Saxon XSLT转换:如何在转换期间失败时关闭输出流

我想用多个输出文件进行XSLT转换。 因为我使用了“xsl:result-document”。 转换失败时,应删除所有输出文件。 但是如果由“xsl:result-document”创建的文档生成失败,我的程序将无法再删除此文档。 我认为原因是,“xsl:result-document”创建了一个不同的OutputStream。 有谁知道如何关闭所有输出流?

编辑:我使用Saxon 9.5进行转换。

请参阅下面的源代码:

public void simpleTransform(String sourcePath, String xsltPath, String outputPath) { String resultDir=outputPath+"/filename.html"; TransformerFactory tFactory = TransformerFactory.newInstance(); StreamSource ss = new StreamSource(new File(xsltPath)); StreamResult sr = new StreamResult(new File(resultDir)); Transformer transformer = tFactory.newTransformer(ss); try { transformer.transform(new StreamSource(new File(sourcePath)), sr); System.out.println("Transformation finished!"); } catch (TransformerException te) { try { System.out.println("Transformation failed! Trying to close Outputstreams..."); sr.getOutputStream().flush(); sr.getOutputStream().close(); transformer.reset(); System.out.println("Outputstream closed!"); try { FileUtils.deleteDirectory(new File(tempDirPath)); System.out.println("Files succesfully deleted!"); } catch(Exception e) { e.printStackTrace(); } } catch(IOException e) { e.printStackTrace(); } } } 

我怀疑你发现了一个错误。 我在这里记录了它:请跟踪它以获得解决方案。

https://saxonica.plan.io/issues/1857

您可以通过注册自己的OutputURIResolver(可能基于标准的)来解决问题,它可以跟踪所有打开的输出流,并且可以由应用程序直接调用以在最后关闭它们。