捉XSL结果文档的输出流(Catch output stream of xsl result-doc

2019-07-17 13:19发布

我需要一种方式,书面方式XSL结果文档,以避免他们书面方式向文件系统造成干扰。 现在我的模板书面方式到一个临时目录,然后我压缩该目录。 我想这样做whitout书面方式向文件系统。 我使用撒克逊。处理器。 即仅使用标准的Java库中的溶液perfered。 任何建议表示赞赏。

编辑:我发现这个类.NET API萨克森http://www.saxonica.com/documentation/dotnetdoc/Saxon/Api/IResultDocumentHandler.html我需要的东西等同为Java。

Answer 1:

您需要实现接口net.sf.saxon.OutputURIResolver
http://www.saxonica.com/documentation/javadoc/net/sf/saxon/lib/OutputURIResolver.html
只要你喜欢,你可以重定向解决方法输出。 在我而言,这是实现类的样子。

public class ZipOutputURIReslover implements OutputURIResolver{

    private ZipOutputStream zipOut;

    public ZipOutputURIReslover(ZipOutputStream zipOut) {
        super();
        this.zipOut = zipOut;
    }

    public void close(Result arg0) throws TransformerException {
        try {
            zipOut.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Result resolve(String href, String base) throws TransformerException {
        try {
            zipOut.putNextEntry(new ZipEntry(href));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new StreamResult(zipOut);
    }
}

在此之后,你需要注册net.sf.saxon.OutputURIResolver到trasnformer工厂。

ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("file.zip"));
factory.setAttribute("http://saxon.sf.net/feature/outputURIResolver", new ZipOutputURIReslover(zipOut));

当您加载模板和运行改造所有的xsl:因此,文件将直接写入数据到ZipOutputSream。

答发现这里http://sourceforge.net/p/saxon/discussion/94027/thread/9ee79dea/#70a9/6fef



Answer 2:

您可以使用new StreamResult(ByteArrayOutputStream())使用这种方法捉XSLT输出,如果你不希望写入文件,然后你可以节省内存中的数据从字节数组到压缩文件中的Java:如何从zip文件byte []数组?



Answer 3:

Note that recent versions of Saxon require that the href (URI) is unique. Thus the system ID of the stream in the output resolver must also be unique.

For example:

  1. Specify the result document href values in the stylesheet

    <xsl:result-document href="{$filename}" method="text">
    
  2. Create the output resolver

    public class ZipOutputURIReslover implements OutputURIResolver{
    
        private ZipOutputStream zipOut;
    
        public ZipOutputURIReslover(ZipOutputStream zipOut) {
            super();
            this.zipOut = zipOut;
        }
    
        public void close(Result arg0) throws TransformerException {
            try {
                zipOut.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public Result resolve(String href, String base) throws TransformerException {
            try {
                zipOut.putNextEntry(new ZipEntry(href));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Result result = new StreamResult(zipOut);
            // Must ensure the stream is given a unique ID
            result.setSystemId(UUID.randomUUID().toString());
            return result;
        }
    }
    
  3. Attach the output resolver to the transformer

    ZipOutputURIResolver outputResolver = new ZipOutputURIResolver(outputStream);
    // Controller is the Saxon implementation of the JAXP Transformer
    ((Controller) transformer).setOutputURIResolver(outputResolver);
    


文章来源: Catch output stream of xsl result-document
标签: java xslt saxon