Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Reference

<xsl:param> Element

Declares a named parameter for use within an <xsl:stylesheet> element or an <xsl:template> element. Allows specification of a default value.

<xsl:param
  name = QName
  select = Expression>
</xsl:param>

Attributes

name
Required. Specifies the name of the parameter.
select
The value of the attribute is an expression, and the value of the variable is the object that results from evaluating the expression. When this attribute is specified, the <xsl:param> element must be empty.

Element Information

Number of occurrences Unlimited
Parent elements xsl:stylesheet, xsl:template, xsl:transform
Child elements xsl:apply-templates, xsl:attribute, xsl:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:processing-instruction, xsl:text, xsl:value-of, xsl:variable, output elements

Remarks

The value specified on the <xsl:param> element is a default value for binding. When the template or style sheet containing <xsl:param> is invoked, parameters are passed that are used in place of the default values.

The <xsl:param> element must be declared as a direct child of an <xsl:template> element. If not declared as a direct child, the value of the <xsl:param> element will not be accessible and an error will occur. For example:

<xsl:template name="getcount">
   <xsl:element name="strong">
      <xsl:param name="counted">
         <xsl:value-of select="count(//book)"/>
      </xsl:param>
      Total Book Count: <xsl:value-of select="$counted"/>
   </xsl:element>
</xsl:template>

In the previous example, the only direct child of the <xsl:template> element is the <strong> element. As such, the value of the <xsl:param> element cannot be correctly evaluated by the parser and results in the following error:

Keyword xsl:param may not be used here.

The correct way to place this element so that it can be evaluated within the context of the <xsl:template> element would be:

<xsl:template name="getcount">
   <xsl:param name="counted">
      <xsl:value-of select="count(//book)"/>
   </xsl:param>
   <xsl:element name="strong">
      Total Book Count: <xsl:value-of select="$counted"/>
   </xsl:element>

The value of the parameter can be an object of any type that can be returned by an expression. The <xsl:param> element can specify the value of the variable in three alternative ways:

Example

This example defines a named template for a "numbered-block" with an argument to control the format of the number.

Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (catmat.xml)

None; the XSLT file calls itself. View the XSLT file in Internet Explorer.

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="paramelem.xsl"?>
<lists>
   <ol>
      <li>the</li>
      <li>cat</li>
      <ol>
         <li>sat</li>
         <li>on</li>
         <li>the</li>
      </ol>
      <li>mat</li>
   </ol>
</lists>

XSLT File (paramelem.xsl)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="paramelem.xsl"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="ol/li">
   <br/>
   <xsl:call-template name="numbered-block"/>
</xsl:template>

<xsl:template match="ol//ol/li">
   <br/>&#xA0;&#xA0;&#xA0;
   <xsl:call-template name="numbered-block">
      <xsl:with-param name="format">a. </xsl:with-param>
   </xsl:call-template>
</xsl:template>

<xsl:template name="numbered-block">
   <xsl:param name="format">1. </xsl:param>
   <fo:block>
      <xsl:number format="{$format}"/>
      <xsl:apply-templates/>
   </fo:block>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

1. the
2. cat
    a. sat
    b. on
    c. the
3. mat

The following is the processor output, with white space added for clarity.

<?xml version="1.0" encoding="UTF-16"?>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">1. the</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">2. cat</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />   
   
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">a. sat</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />   
   
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">b. on</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />   
   
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">c. the</fo:block>
<br xmlns:fo="http://www.w3.org/1999/XSL/Format" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">3. mat</fo:block>

See Also

<xsl:with-param> Element | <xsl:variable> Element | <xsl:call-template> Element