Project

Profile

Help

Transformation with packaged stylesheet from Java

Added by Vivian Steller almost 10 years ago

Hi, I've trouble getting a transformation with a packaged stylesheet to work. I managed to package the stylesheet as a ZIP-file as described "in the documentation":http://www.saxonica.com/documentation/index.html#!using-xsl/packaged-xslt . I also set @PackageURIResolver@ as URIResolver using:

		processorConfig.setURIResolver(new com.saxonica.ptree.PackageURIResolver());
		final net.sf.saxon.s9api.Processor processor = new net.sf.saxon.s9api.Processor(processorConfig);

Now the questions:

How do I compile a packaged stylesheet?

Do I have to compile at all?

How to obtain a reference to an @XsltExecutable@?

Thanks for any hints! Cheers, Vivian


Replies (4)

Please register to reply

RE: Transformation with packaged stylesheet from Java - Added by Michael Kay almost 10 years ago

The only documentation on how to run a packaged stylesheet is here

http://www.saxonica.com/documentation/index.html#!using-xsl/packaged-xslt

and it only tells you how to do it from the command line. I'm afraid this is deliberate, because although it is of course possible to do it from Java code, it's not "idiot-proof" and there are potential pitfalls. The main one is that the packaged stylesheet cannot share a Configuration with anything else, mainly because it requires a dedicated NamePool.

Here's a unit test that shows a packaged stylesheet being run from Java code, which I hope will help:

public void testPackageLoading() { Processor processor = new Processor(false); XsltCompiler compiler = processor.newXsltCompiler();

    PackageURIResolver resolver = new PackageURIResolver();

    compiler.setURIResolver(resolver);
    resolver.setConfiguration(processor.getUnderlyingConfiguration());
    try {
        Source source = resolver.resolve(ConfigTest.DATA_DIR + "booksPackage.zip", ConfigTest.DATA_DIR);
        XsltTransformer transformer = compiler.compile(source).load();

        transformer.setSource(new StreamSource(ConfigTest.DATA_DIR + "books.xml"));

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Serializer serializer = processor.newSerializer(os);

        transformer.setDestination(serializer);
        transformer.transform();
    } catch (SaxonApiException e) {
        e.printStackTrace();
        fail();
    } catch (XPathException e) {
        e.printStackTrace();  
        fail();
    }


}

RE: Transformation with packaged stylesheet from Java - Added by Vivian Steller almost 10 years ago

Thank you very much! This basically works well. However, is there any way to read the packaged stylesheet ZIP from the classpath? It seems problematic since PackageURIResolver seems to use ZipFile instead of ZipInputStream o.e., right?

Loading the stylesheet ZIP from the classpath has the advantage that production and test code can use the exact same code base.

What do you think? If this is not possible right now, do you think there is a chance to fix this in an upcoming release of Saxon 9.5?

Thanks again!

RE: Transformation with packaged stylesheet from Java - Added by Michael Kay almost 10 years ago

I can certainly see the use case for reading from the classpath, however, converting it to use a ZipInputStream would not be a trivial enhancement, since Saxon reads entries from the zip file by name and ZipInputStream, as far as I can see, only provides sequential access - which means we would have to build our own data structure mapping names to content.

Is it really needed anyway? Have you tried supplying a classpath URL, for example

classpath:org/my/package/compiled-stylesheet.zip

I'm not saying this will work, but it seems worth trying.

RE: Transformation with packaged stylesheet from Java - Added by Vivian Steller almost 10 years ago

Thanks for your response. I see your point with ramdom access. I also tried the classpath URL, along with registering a classpath protocol handler. Unfortunately without success, Saxon is not satisfied with this.

Anyway, I worked around the issue by temporarily extracting the stylesheet.zip from the classpath into a temporary file. Then, I pass the temporary file to Saxon. Then the stylesheet is compiled it seems to be cached, hence I delete the ZIP file again after the first transformation. This works quite fine for both the production WAR deployment and for test cases.

    (1-4/4)

    Please register to reply