kantesh
kantesh

Reputation: 73

Saxonica Generate SEF file from xslt and apply the same for transformation

I am trying to find/know the correct approach to save sef in memory and use the same for transformation

Found below two approaches to generate sef file:

1. using xsltpackage.save(File) : it works fine but here need to save content to a File which doesn't suit our requirement as we need store in memory/db.

2. XsltExecutable.export() : it generated file but if i use the same .sef file for transformation, i am getting empty content as output(result).

I use xsl:include and document in xslt and i resolved them using URI resolver.

I am using below logic to generate and transform.

Note: i am using Saxon ee (trial version).

1.XsltExecutable.export()

 public static String getCompiledXslt(String xsl, Map<String, String> formatterMap) throws SaxonApiException, IOException {     
        
        try(ByteArrayOutputStream destination = new ByteArrayOutputStream()){
        Processor processor = new Processor(true);
        XsltCompiler compiler = processor.newXsltCompiler();
        compiler.setURIResolver(new CigURIResolver(formatterMap));
        XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(xsl)));
        
        
        stylesheet.export(destination);
        return destination.toString();
        }catch(RuntimeException ex) {
            throw ex;
        }
    }

use the same SEF for transformation:

    Processor processor = new Processor(true);
    XsltCompiler compiler = processor.newXsltCompiler();
    if (formatterMap != null) {
        compiler.setURIResolver(new CigURIResolver(formatterMap));
    }
    
    
    XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(standardXsl)));
    Serializer out = processor.newSerializer(new File("out4.xml"));
    out.setOutputProperty(Serializer.Property.METHOD, "xml");
    out.setOutputProperty(Serializer.Property.INDENT, "yes");
     
    Xslt30Transformer trans = stylesheet.load30();
    if (formatterMap != null) {
    trans.setURIResolver(new CigURIResolver(formatterMap));
    }
    
    trans.transform(new StreamSource(new StringReader(sourceXMl)), out);

    System.out.println("Output written to out.xml");

} 

when use the sef generated from above export method to transform , i am getting empty content..same code works fine with sef generated from XsltPackage.save().

UPDATE : solved the issue by setting false to property (by default it is true) compiler.setJustInTimeCompilation(false);

Upvotes: 1

Views: 346

Answers (1)

Michael Kay
Michael Kay

Reputation: 163488

There's very little point (in fact, I would say there is no point) in saving a SEF file in memory. It's much better to keep and reuse the XsltExecutable or XsltPackage object rather than exporting it to a SEF structure and then reimporting it. The only reason for doing an export/import is if the exporter and importer don't share memory.

You can do it, however: I think the only thing you need to change is that you need to close the destination stream after writing to it. Saxon tries to stick to the policy "Anyone who creates a stream is responsible for closing it"

Upvotes: 1

Related Questions