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 xmlns:xsl="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. 

Comments:

Post a Comment:
Comments are closed for this entry.
About

cwebster

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today