Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Passing Values into XSLT Using Global Parameters

To pass a value into an XSLT style sheet at run time, you must perform the following steps.

  1. Declare a global XSLT parameter in the style sheet.
  2. Call the addParameter method on the XSLProcessor object, from a script, to assign a value to the parameter.

Step 1 is straightforward: Declare <xsl:param> as an immediate child element of the <xsl:stylesheet> element. For example:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="bgColor">black</xsl:param>

<xsl:template match="/">

bgColor = <xsl:value-of select="$bgColor"/>

</xsl:template>

</xsl:stylesheet>

The example above defines a global parameter, bgColor. The value of this parameter will be displayed as a string.

Step 2 is more complicated. We need to create an XSLTemplate instance to cache the compiled style sheet, and an XSLProcessor object that will be used to pass a value (red) to the global parameter (bgColor) declared in the style sheet. The following is an example of Step 2.

<HTML>
<HEAD>
   <TITLE>bgColor in HTML</TITLE>
</HEAD>

<SCRIPT>
  var xslt, xml, xslTemp, xslProc;
  function load()
  {
     xml  = new ActiveXObject("Msxml2.DOMDocument.5.0");
     xml.async=false;
     xml.load("bgColor.xsl");

     xslt = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
     xslt.async=false;
     xslt.load("bgColor.xsl");

     xslTemp = new ActiveXObject("Msxml2.XSLTemplate.5.0");
     xslTemp.stylesheet = xslt;
     
     xslProc = xslTemp.createProcessor();
     xslProc.input = xml;
     xslProc.addParameter("bgColor", "red");

     try { 
       xslProc.transform;
       result.innerHTML = xslProc.output;
     }
     catch(e)
     {
       result.innerHTML = e.description;
     }
  }
</SCRIPT>

<BODY ONLOAD="load()">
  <DIV ID="result"></DIV>
</BODY>
</HTML>

See Also

Increasing Performance by Using the XSLTemplate Object| addParameter Method