Efficient XSLT pipeline, with params, in Java

The top answer to this question describes a technique to implement an efficient XSLT pipeline in Java:

Efficient XSLT pipeline in Java (or redirecting Results to Sources)

Unfortunately, while Transformer seems to expose an API for setting XSLT parameters, this does not seem to have any effect. For example, I have the following code:

Transformer.java

import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.sax.TransformerHandler; 
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Transformer;
import java.io.File;
public class MyTransformer {
    public static void main(String[] args) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException{
        SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();

        // These templates objects could be reused and obtained from elsewhere.
        Templates templates1 = stf.newTemplates(new StreamSource( new File("MyStylesheet1.xslt")));
        Templates templates2 = stf.newTemplates(new StreamSource(new File("MyStylesheet2.xslt")));

        TransformerHandler th1 = stf.newTransformerHandler(templates1);
        TransformerHandler th2 = stf.newTransformerHandler(templates2);

        th1.setResult(new SAXResult(th2));
        th2.setResult(new StreamResult(System.out));

        Transformer t = stf.newTransformer();

            //SETTING PARAMETERS HERE
        t.setParameter("foo","this is from param 1");
        t.setParameter("bar","this is from param 2");

        t.transform(new StreamSource(new File("in.xml")), new SAXResult(th1));

        // th1 feeds th2, which in turn feeds System.out.
    }
}

MyStylesheet1.xslt



    

    

    

    

MyStylesheet2.xslt



    

    

    

    

in.xml


    

Which gives me the following output:


        foo is : 

As you can see foo:world/@attr is empty, and the text contents of foo:world says "foo is:". The expected behaviour is that they should have been populated with the parameters passed into the setParameter method.

Is there a way to set XSL transformation parameters using this technique. If not, could anyone recommend an alternative technique for transforming stylesheets efficiently in Java, such that XSLT parameters may also be set?

5
задан Community 23 May 2017 в 11:46
поделиться