Chris Webster's Weblog
How to pass a parameter into an XSLT stylesheet
First step is to set the parameter in Java. Here is some sample code:
TransformerFactory tf = TransformerFactory.newInstance();
File transform = new File("myTransform.xsl");
Transformer t = tf.newTransformer(new StreamSource(transform));
t.setParameter("aParam", "value");
t.transform(new StreamSource(new File("input.xml")), new StreamResult(new File("output.xml")));⁞
Above is the basic code to get a stylesheet working, so performance could be improved by compiling the stylesheet using TransformerFactory.newTemplates. The Transformer.setParameter call is what actually passes the parameter into the stylesheet.
The next step is to start using the parameter in the stylesheet. The parameter is treated just like an param element so this can be accessed by adding a param element as a child of the stylesheet element.
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="aParam"/> ...
Now this can used within the stylesheet just like a parameter.
Posted at 10:28AM Nov 14, 2008 by Christopher Webster in Java |
Friday Nov 14, 2008
sl="http://www.w3.org/1999/XSL/Transform"