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>
<xsl:param>
element must be empty.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 |
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:
select
attribute, the value of the attribute must be an expression and the value of the parameter is the object that results from evaluating the expression. In this case, the content of the element must be empty.select
attribute and has non-empty content such as one or more child nodes, the content specifies the value. The content is a template that is instantiated to give the value of the parameter. The value is a result tree fragment equivalent to a node-set containing just a single root node having as children the sequence of nodes produced by instantiating the template. The base URI of the nodes in the result tree fragment is the base URI of the element.
An error occurs if a member of the sequence of nodes created by instantiating the template is an attribute node or a namespace node, because a root node cannot have an attribute node or a namespace node as a child.
select
attribute, the value of the parameter is an empty string. Thus
<xsl:
paramname="x"/>
is equivalent to
<xsl:param name="x" select="''"/>
Note When a parameter is used to select nodes by position, be careful not to do the following:
<xsl:param name="n">2</xsl:param>
...
<xsl:value-of select="item[$n]"/>
This will output the value of the first item element, because the variable "n" will be bound to a result tree fragment, not a number. Instead, do either
<xsl:param name="n" select="2"/> ... <xsl:value-of select="item[$n]"/>
or
<xsl:param name="n">2</xsl:param> ... <xsl:value-of select="item[number($n)]"/>
Note The following is a convenient way to specify the empty node-set as the default value of a parameter.
<xsl:param name="x" select="/.."/>
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/>    <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>
<xsl:with-param> Element | <xsl:variable> Element | <xsl:call-template> Element