Is there any way to pass parameters to XSL from outside the XSL, such as from a JSP?

Peter Ciuffetti

The XSLT spec says that top-level xsl:param's and xsl:variable's can be set by the processor at run-time, but its up to the processor to provide a mechanism for doing so. These mechanisms are processor specific.

The Xalan XSLT processor, available at, has a method for setting top-level parameters using either the command line mode or the Java API it provides. The following JSP code shows how to access Xalan from within a JSP and then how to override a top-level parameter at runtime.

So the test would be self-contained, I've stored my XML and XSL inputs as strings. These would normally be files outside the JSP code accessed via URLs or via file reads.

In the XSL, you'll notice it has a top-level (that is, it is a direct child of the xsl:stylesheet element) xsl:param named 'theParam'.

It's default value is a string 'the Default'. The stylesheet has a single match pattern for the root of the test document and it outputs an HTML heading that echo's the value of the 'theParam'.

After setting up the inputs to the Xalan processor, and just before doing the transformation, there's is a call to xp.setStylesheetParameter(..). This sets the value of the top level parameter 'theParam' to the new value 'the Override'. The processor places the transformation result in a string and the JSP outputs this string to the HTML output.

Without the setStylesheetParameter() line, the output would be:

<h1>Our param is: the Default</h1>

With this line, the output from the same stylesheet would be:

<h1>Our param is: the Override</h1>

To run this test, install Xalan and add xalan.jar and xerces.jar to the classpath of your jsp engine. Then put the jsp file below in the document space of the jsp engine. Load the JSP on your browser and you should see the heading above.

----- TestXSLT.jsp -----
<head><title>Test XSLT</title></head>
<%@ page import="org.apache.xalan.xslt.*"%><%@ page import="*"%><%

    XSLTProcessor xp = null;
    try {
        xp = XSLTProcessorFactory.getProcessor();
    } catch ( Exception e ) {
        %><h1>Unable to obtain processor</h1>

    String inputDoc = new String("<?xml version="1.0"?><root><test></test></root>");
    String styleSheet = new String(
    "<xsl:stylesheet xmlns:xsl=""><xsl:param name="theParam" select="'the default'"/><xsl:output method="html"/><xsl:template match="/"><h1>Our param is: <xsl:value-of select="$theParam"/></h1></xsl:template></xsl:stylesheet>");
    StringWriter resultStr = new StringWriter();

    XSLTInputSource xml = new XSLTInputSource(new StringReader(inputDoc));
    XSLTInputSource xsl = new XSLTInputSource(new StringReader(styleSheet));
    XSLTResultTarget rt = new XSLTResultTarget(resultStr);

    if ( xp != null && xml != null && xsl != null && rt != null ) {
        try {
            xp.setStylesheetParam("theParam", "'the Override'");
            xp.process(xml, xsl, rt);
        } catch ( Exception e ) {
            %><h1>Unable to perform transformation</h1>
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact