Question

I am producing compiled .class files (Translet) from XSL transformation files with using TransformerFactory which is implemented by org.apache.xalan.xsltc.trax.TransformerFactoryImpl.

Unfortunately, I couldn't find the way how to use these translet classes on XML transformation despite my searchings for hours.

Is there any code example or reference documentation may you give? Because this document is insufficient and complicated. Thanks.

Was it helpful?

Solution

A standard transformation in XSLT looks like this:

    public void translate(InputStream xmlStream, InputStream styleStream, OutputStream resultStream) {
        Source source = new StreamSource(xmlStream);
        Source style = new StreamSource(styleStream);
        Result result = new StreamResult(resultStream);

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer t = tFactory.newTransformer(style);
        t.transform(source, result);
    }

so given that you don't use a Transformer factory, but a ready made Java class (which is an additional maintenance headache and doesn't give you that much better performance since you can keep your transformer object after the initial compilation) the same function would look like this:

    public void translate(InputStream xmlStream, OutputStream resultStream) {
        Source source = new StreamSource(xmlStream);
        Result result = new StreamResult(resultStream);

        Translet t = new YourTransletClass();
        t.transform(source, result);
    }

In your search you missed out to type the Interface specification into Google where the 3rd link shows the interface definition, that has the same call signature as Transformer. So you can swap a transformer object for your custom object (or keep your transformer objects in memory for reuse)

Hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top