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 Sou
The problem is that each TransformerHandler has a separate Transformer associated with it. There's a problem with your 2nd template, but as this is an example I guess that doesn't matter. You want:
//SETTING PARAMETERS HERE
th1.getTransformer().setParameter("foo","this is from param 1");
th2.getTransformer().setParameter("bar","this is from param 2");
Note that you also don't need to create a 3rd transformer you can just start the transform chain on th1.getTransformer()
related to the last note. invoking transform() on th1.getTransformer() with the result pointing again on th1 is not correct. It will be processed twice. using new Transformer() like shown in the initial post is the correct way.
t.transform(new StreamSource(new File("in.xml")), new SAXResult(th1));