XSLT » Elements » xsl:param


The xsl:param element is used to declare a local or global parameter and to give that parameter a name and a default value. The default value will be used only if no other value is provided when the template is called.

The default value can be assigned by either the content of the xsl:param element or by the select attribute, but not by both. Each parameter declaration requires a separate xsl:param element. Global parameters are declared in the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements). Local parameters are declared by using the xsl:param element as a child of the xsl:template element.

The actual (explicit) value is set by using xsl:with-param element when the template is applied (invoked) by either the xsl:apply-template or the xsl:call-template elements.

The xsl:variable element can also be used to declare local and global variables. The only real difference between a variable and a parameter is how the value is assigned.

Like all XSLT elements, the xsl:param element must be closed (well-formed). If the select attribute is present, then this element is self-closing. If the select attribute is not present, then this element is not self-closing and the separate closing element is mandatory.



<xsl:template name="hoo" match="/">
  <xsl:for-each select="devguru_staff/programmer">
      <xsl:with-param name="myname"select="name" />
      <xsl:with-param name="mydob"select="dob" />

<xsl:template name="boo">
  <xsl:param name="myname"select="'Not Available'" />
  <xsl:param name="mydob"select="'Not Available'" />
  NAME: <xsl:value-of select="$myname" />
  <br />
  DOB: <xsl:value-of select="$mydob" />
  <hr />

NAME: Bugs Bunny
DOB: 03/21/1970
NAME: Daisy Duck
DOB: 08/09/1949
NAME: Minnie Mouse
DOB: 04/13/1977

This is the code for xslt_example_param.xsl. The output has been truncated.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_param.xsl"?>

Language(s): XSLT

See Also: