<xsl:with-param> |
Defines a parameter to be passed to a template. When the template is invoked, values can be passed in for the parameter.
Category
Subinstruction (<xsl:with-param> always appears inside an <xsl:apply-templates> or <xsl:call-template> element)
Description
<xsl:with-param> defines a parameter to be passed to a template. When the template is invoked, values can be passed in for the parameter. The value of the parameter can be defined in one of three ways:

  If the <xsl:with-param> element is empty and does not contain a select attribute, then no value is passed to the template.
  If the <xsl:with-param> element is empty and has a select attribute, the value of the parameter is the value of the select attribute.
  If the <xsl:with-param> element contains an XSLT template, the value of the parameter is the result of processing the template.

If no value is passed to the template (<xsl:with-param name="x"/>), then the default value of the parameter, if any, is used instead. The default value of the parameter is defined on the <xsl:param> element inside the <xsl:template> itself; see the description of the <xsl:param> element for more details.

Required Attributes

name
  Names this parameter.

Optional Attributes

select
  An XPath expression that defines the value of this parameter.

Content
The <xsl:with-param> element can be empty, or it can contain an XSLT template. If it contains an XSLT template, the value of the select attribute (if any exists) is ignored.
Appears in
<xsl:apply-templates> and <xsl:call-template>.
Defined in
XSLT section 11.6, Passing Parameters to Templates.
Example
Here is a stylesheet with a number of parameters. Notice that some parameters are global and defined outside the stylesheet:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:output method="text"/>

      <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
      </xsl:variable>

      <xsl:param name="favoriteNumber" select="23"/>
      <xsl:param name="favoriteColor"/>

      <xsl:template match="/">
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="list/title"/>
        <xsl:value-of select="$newline"/>
        <xsl:variable name="listitems" select="list/listitem"/>
        <xsl:call-template name="processListitems">
          <xsl:with-param name="items" select="$listitems"/>
          <xsl:with-param name="color" select="'yellow'"/>
          <xsl:with-param name="number" select="$favoriteNumber"/>
        </xsl:call-template>
      </xsl:template>

      <xsl:template name="processListitems">
        <xsl:param name="items"/>
        <xsl:param name="color" select="'blue'"/>

        <xsl:for-each select="$items">
          <xsl:value-of select="position()"/>
          <xsl:text>.  </xsl:text>
          <xsl:value-of select="."/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>

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

        <xsl:text>Your favorite color is </xsl:text>
        <xsl:value-of select="$favoriteColor"/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:text>The color passed to this template is </xsl:text>
        <xsl:value-of select="$color"/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select="$newline"/>
      </xsl:template>

    </xsl:stylesheet>
We'll use this stylesheet to transform this document:
    <?xml version="1.0"?>
    <list xml:lang="en">
      <title>Albums I've bought recently:</title>
      <listitem>The Sacred Art of Dub</listitem>
      <listitem>Only the Poor Man Feel It</listitem>
      <listitem>Excitable Boy</listitem>
      <listitem xml:lang="sw">Aki Special</listitem>
      <listitem xml:lang="en-gb">Combat Rock</listitem>
      <listitem xml:lang="zu">Talking Timbuktu</listitem>
      <listitem xml:lang="jz">The Birth of the Cool</listitem>
    </list>
Our stylesheet contains two global parameters, favoriteNumber and favoriteColor, and defines a default value for favoriteNumber. The stylesheet also passes a parameter from the match="/" template to the name="processListitems" template; that parameter contains a node-set. Here are the results of the transformation:
    Albums I've bought recently:
    1.  The Sacred Art of Dub
    2.  Only the Poor Man Feel It
    3.  Excitable Boy
    4.  Aki Special
    5.  Combat Rock
    6.  Talking Timbuktu
    7.  The Birth of the Cool

    Your favorite color is orange.
    The color passed to this template is yellow.
To generate these results with Xalan, we use this command:
    java org.apache.xalan.xslt.Process -in test4.xml -xsl with-param.xsl
      -param favoriteColor orange
The command should appear on a single line. See Section 4.4.3 in Chapter 4 for a complete discussion of global parameters and how you define them for various XSLT processors.