Reading xsl transform variables from Java

booher1

New Member
I know that I can pass parameters from Java into an xsl transform, but how can I read variables/parameters back out after the transform is complete?For example, I have an xml document like this:\[code\]<author> <name>Barry Bonds</name> <books> <book>Hitting Home Runs</book> <book>Cheating at Baseball</book> <book>Nobody Likes Cooperstown Anyway</book> <books><author>\[/code\]In a single transform, I'd like to be able to transform the name element and store that as a variable and also transform the book elements and store them as a second variable. Then I would like to get access to those 2 variables from Java code, after the transform.Is this possible, and if so, how is it done?Thanks to Michael Kay's answer, I now have a solution. For others who come across this, here's a unit test showing how it works:\[code\]import net.sf.saxon.*;import org.junit.Test;import javax.xml.transform.Result;import javax.xml.transform.Source;import javax.xml.transform.TransformerException;import javax.xml.transform.stream.StreamResult;import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import static org.junit.Assert.assertEquals;public class SimpleTransform { @Test public void splitOutput() throws TransformerException, UnsupportedEncodingException { final Map<String, StreamResult> results = new HashMap<String, StreamResult>(); results.put("test1", new StreamResult(new ByteArrayOutputStream())); results.put("test2", new StreamResult(new ByteArrayOutputStream())); results.put("test3", new StreamResult(new ByteArrayOutputStream())); TransformerFactoryImpl factory = new TransformerFactoryImpl(); Source source = new StandardURIResolver(new Configuration()).resolve("transform.xslt", null); Controller xsl = (Controller) factory.newTransformer(source); xsl.setOutputURIResolver(new OutputURIResolver() { @Override public Result resolve(String s, String s1) throws TransformerException { return results.get(s); } @Override public void close(Result result) throws TransformerException { } }); Source xsd = (Source) Transform.loadDocuments("input.xml", false, null, false); StreamResult standardResult = new StreamResult(new ByteArrayOutputStream()); xsl.transform(xsd, standardResult); for (Map.Entry<String, StreamResult> stringStreamResultEntry : results.entrySet()) { System.out.println(stringStreamResultEntry.getKey() + ": " + ((ByteArrayOutputStream)stringStreamResultEntry.getValue().getOutputStream()).toString("UTF-8")); } assertEquals("<html>\n <body>test1</body>\n</html>", ((ByteArrayOutputStream) results.get("test1").getOutputStream()).toString("UTF-8")); assertEquals("<html>\n <body>test2</body>\n</html>", ((ByteArrayOutputStream) results.get("test2").getOutputStream()).toString("UTF-8")); assertEquals("<html>\n <body>test3</body>\n</html>", ((ByteArrayOutputStream) results.get("test3").getOutputStream()).toString("UTF-8")); }}\[/code\]
 
Back
Top