Sponsored

XSLT Reference

xsl:param

XSLT 1.0 element

Declares a parameter with an optional default value, accepting values passed via xsl:with-param or from the calling environment.

Syntax
<xsl:param name="name" select="expression" as="type" required="no"/>

Description

xsl:param declares a named parameter. It is syntactically similar to xsl:variable, but its value can be overridden by the caller at the point of invocation. If the caller does not supply a value, the default expressed by select or element content is used instead.

Like xsl:variable, xsl:param can appear at two levels:

  • Top-level (direct child of xsl:stylesheet): declares a global stylesheet parameter. XSLT processors typically allow the host environment (a Java application, a command-line tool, or a browser) to supply values for top-level parameters at runtime.
  • Inside xsl:template: declares a template parameter. The value is supplied by xsl:with-param in the calling xsl:apply-templates or xsl:call-template.

Once set for a given invocation, a parameter is immutable — you cannot reassign it inside the template body, just as with xsl:variable.

Attributes

AttributeTypeRequiredDescription
nameQNameYesThe parameter name, referenced as $name in XPath.
selectXPath expressionNoDefault value expression. Used when caller does not provide a value.
asSequenceTypeNo(2.0+) Expected type for type-checking.
requiredyes / noNo(2.0+) If yes, raises an error when no value is passed. Default no.
tunnelyes / noNo(2.0+) Tunnel parameters bypass intermediate templates automatically.

Examples

Template parameter with default

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<message>Hello</message>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template name="greet">
    <xsl:param name="name">World</xsl:param>
    <xsl:param name="prefix" select="'Hi'"/>
    <xsl:value-of select="concat($prefix, ', ', $name, '!')"/>
  </xsl:template>

  <xsl:template match="/message">
    <xsl:call-template name="greet">
      <xsl:with-param name="name">Saxon</xsl:with-param>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

Output:

Hi, Saxon!

Top-level stylesheet parameter

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<report><total>1500</total></report>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="currency">USD</xsl:param>
  <xsl:param name="multiplier" select="1"/>

  <xsl:template match="/report">
    <result>
      <amount>
        <xsl:value-of select="format-number(total * $multiplier, '#,##0.00')"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$currency"/>
      </amount>
    </result>
  </xsl:template>
</xsl:stylesheet>

Output (with default parameters):

<result>
  <amount>1,500.00 USD</amount>
</result>

Notes

  • A template parameter that is not passed by the caller silently uses its default value. In XSLT 1.0, there is no way to make a parameter required at the language level; you must check with xsl:if and emit xsl:message if needed.
  • xsl:param must appear before any other instructions in a template body; placing it after variable declarations or output instructions is an error.
  • Global parameters supplied by the host environment are typically strings. Numeric or node-set values may require explicit conversion in the stylesheet.
  • In XSLT 2.0+, the required="yes" attribute provides a first-class mechanism for mandatory parameters, removing the need for manual guard checks.

See also