XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / param

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The instruction xsl:param defines a named parameter whose value can be supplied externally, overriding any default. Its value cannot then change during its scope. A parameter may be global to the stylesheet or local to a template rule.

Classification

Declaration/instruction

Function group

Parameter definition

Introduced in

XSLT 1.0

Stylesheet position and permitted contents:

xsl:param occurs both as a top-level element and an instruction. As a top-level child of xsl:stylesheet or xsl:transform, it creates a global parameter. As an instruction within a template rule xsl:template , it creates a local parameter valid in that rule.

The element must be empty if its select attribute is used. Without a select attribute, it contains a template body, a sequence constructor combining instructions, text and literal result elements freely. Instead of targeting the result document, this constructor creates a temporary tree accessible through the parameter.

Attributes:

Standard attributes apply. In addition, xsl:param has a required name attribute and an optional select attribute. XSLT 2.0 adds optional attributes as, required and tunnel.

as

Value

sequence-type

Usage

optional

Introduced in

XSLT 2.0

The as attribute defines the parameter value’s datatype as an XPath 2.0 sequence type. If as is set, the value must match that type.

Otherwise conversion is attempted; failure is a type error (ERR XTTE0590). Without an as attribute, the value is taken unchanged from the evaluated select expression or the contained sequence constructor.

If an as attribute exists but neither a select attribute nor a sequence constructor supplies a default, the default is an empty sequence, not the usual empty string. If the as datatype disallows an empty sequence, the parameter is considered required: a runtime error ERR XTDE0610 aborts processing if no value is passed and the default applies. If a value is always passed, the processor need not report an existing default’s type mismatch.

Note: The as attribute for xsl:param does not exist in XSLT 1.0.

name

Value

qname

Usage

required

Introduced in

XSLT 1.0

The required name attribute names the parameter for stylesheet references. It is a QName. Same-named parameters must not share an import precedence or scope (Scope): no same-named globals or locals in one rule. Violations are static errors (ERR XTSE0580). The same applies to same-named parameters in a function declared with xsl:function.

Same names are allowed in these cases:
Same-named global parameters must have different import precedence. If importing a module creates duplicates, the highest-precedence parameter wins, normally the main module’s. Lower-precedence values are inaccessible.

A global and a local parameter can share a name. The local parameter shadows (shadows) its global counterpart within its rule, making the global value inaccessible there.

Local parameters in different template rules can share names because their scopes do not overlap. Each exists only while its rule runs and is inaccessible outside it.

required

Value

"yes" | "no"

Usage

optional

Introduced in

XSLT 2.0

The attribute required determines whether a value must be passed.

  • required="no" (default)
    If the required attribute is "no", a value need not be passed. If none is supplied, the default from the evaluated select attribute or sequence constructor applies.

  • required="yes"
    If the required attribute is set to "yes", a value must always be passed. No default is allowed because it could never apply; xsl:param must be empty and cannot have a select attribute.

Note: The required attribute does not exist in XSLT 1.0.

Caution — Special case in XSLT 2.0 xsl:function declarations:
The required attribute is forbidden when xsl:param defines function parameters inside xsl:function. Values are required anyway but are supplied through the function call, not through xsl:with-param.

select

Value

xpath-expression

Usage

optional

Introduced in

XSLT 1.0

The select attribute alternatively replaces a sequence constructor inside xsl:param to create the default value. It contains an XPath expression yielding the desired default. With a select attribute, the element must be empty, although the specification does not explicitly require an error.

Evaluation produces a sequence. Numeric literals can be passed directly; string literals require quotation marks in addition to attribute delimiters:

<xsl:param name="stringparam" select="'mein stringwert'"/>

With neither a select attribute nor an internal sequence constructor, the default is an empty string. These expressions are therefore equivalent:

<xsl:param name="leerer_param"/>

<xsl:param name="leerer_param" select="''"/>

Caution — Special case in XSLT 2.0 xsl:function declarations:
If xsl:param receives a function argument inside an xsl:function declaration, it may contain neither a template body nor a select attribute.

tunnel

Value

"yes" | "no"

Usage

optional

Introduced in

XSLT 2.0

The attribute tunnel with value "yes" marks a tunnel parameter receiving its value from the tunnel parameter stream. It cannot be used for top-level global parameters or function parameters.

Note: The tunnel attribute does not exist in XSLT 1.0.

Purpose:

The instruction xsl:param declares parameters comparable to XSLT variables created with xsl:variable. Like those, these are static variables whose values cannot change during their scope.

Unlike a variable, a parameter can receive an external value when declared, overriding its default. A variable, in this sense, has only a nonoverridable default.

A parameter named name="p_name" is referenced in XPath or AVTs like a variable, prefixing its name with $: $p_name.

Caution — No difference from xsl:variable when referencing:
XPath and AVT references do not distinguish variables and parameters. Whether $x was declared with <xsl:variable name="x"> or <xsl:param name="x"> is invisible in the reference. Name collisions and shadowing therefore also occur between variables and parameters.

Like a variable, a parameter has a select attribute or template body evaluated to obtain its value. Unlike a variable, this is only a default that can be overridden externally at declaration.

Parameters are global or local according to their scope, determined by declaration position.

Global parameters:

If the declaration xsl:param is a direct child of xsl:stylesheet or xsl:transform, the parameter is global throughout the stylesheet’s template rules and can be referenced everywhere there:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="para_global" select="'defaultwert'"/>
  <!-- Referenzierung überall im Stylesheet mit $para_global -->
</xsl:stylesheet>

An exception is a rule defining a same-named local parameter or variable, shadowing the global parameter and hiding its value within that rule.

Passing values to global parameters:
Initialization timing depends on declaration position. A global parameter is initialized when the stylesheet is invoked and retains its value throughout transformation; runtime reassignment is impossible.

How global values are passed depends on the transforming application: via query string, such as Cocoon, or command-line invocation, such as Saxon or XT.

  • For  Cocoon the syntax is roughly:
    "http://www.bei­spiel.org/beispiel.xml?p_name=p_wert"
    The parameter goes to the processing stylesheet, not to the XML document being requested.

  • For Saxon 8, a command line passing two parameters is:
    java net.sf.saxon.Transform beispiel.xml beispiel.xsl  p_name=p_wert p_name2=p_wert2
    Both methods pass a name-value pair. The stylesheet accesses the value with $p_name like a variable. A global xsl:param element with the chosen name name="p_name" must exist or the value is discarded.

Local parameters:

A parameter declared as an instruction within a template rule xsl:template is local during that rule’s execution. The xsl:param element must be the first child, before all other template content. Several xsl:param elements may occur consecutively; their order affects neither value passing nor references. (Note: This differs in function declarations with xsl:function.)

<xsl:template match="beispiel">
  <xsl:param name="param1" select="'defaultwert1'"/>
  <xsl:param name="param2" select="'defaultwert2'"/>
  ...
  <!-- Referenzierung mit $param1 und $param2 -->
</xsl:template>

Every rule invocation reinitializes its local parameters. Values remain unchanged while the rule runs, but may or must be supplied anew on each invocation, possibly with different values. If none is passed, the default applies.

Local parameters do not exist before or after rule instantiation and cannot be accessed outside the rule, including by subordinate rules it calls.

Other rules can safely use same-named parameters since scopes do not overlap. Within its rule, a local parameter overrides a same-named global parameter or variable.

Passing values to local parameters:
Local parameter values are passed with the XSLT instruction xsl:with-param. Its name attribute names the destination parameter; its select attribute or template body supplies the value. The target rule should declare that parameter. Otherwise, unless it is a tunnel parameter, the value is discarded without a runtime error, except when passed from xsl:call-template.

Values are passed when the containing rule is invoked, either through xsl:apply-templates (in XSLT 2.0 also from xsl:apply-imports or xsl:next-match) or xsl:call-template. Use one xsl:with-param child instruction per parameter value; their order does not matter.

Passing values from  xsl:apply-templates:

...
<xsl:apply-templates select="beispiel">
  <xsl:with-param name="sinn-des-lebens" select="42"/>
  <xsl:with-param name="autor">Douglas Adams</xsl:with-param>
</xsl:apply-templates>
...

The xsl:with-param element generates a temporary tree from its template body, accessible through the parameter reference inside the rule. Otherwise, it passes the XPath result from its select attribute.

If one xsl:apply-templates instruction addresses several rules, for instance using a heterogeneous node set, they all receive the same parameter values.

Passing values from  xsl:call-template:

...
<xsl:call-template name="meine_templateregel">
  <xsl:with-param name="sinn-des-lebens" select="42"/>
  <xsl:with-param name="autor">Douglas Adams</xsl:with-param>
</xsl:call-template>
...

Value passing follows the same principle as xsl:apply-temp­lates.

Caution — Difference between XSLT 1.0 and 2.0:
In XSLT 2.0 outside backwards-compatible mode, passing a value without a matching local target parameter is a static error (ERR XTSE0680). XSLT 1.0 simply discards the value.

Passing values from  xsl:apply-imports (available from XSLT 2.0):

<xsl:template select="beispiel">
  <xsl:param name="para_1" select="defaultwert"/>
  ...
  <xsl:apply-imports>
    <xsl:with-param name="para_1" select="$para_1"/>
  </xsl:apply-imports>
  ...
</xsl:template>

In XSLT 1.0, activated imported rules had to use their local defaults because values could not be passed. XSLT 2.0 extends the content model of xsl:apply-imports to allow parameter values.

Since the instruction xsl:apply-imports, unlike the activated rule, is in scope of the caller’s local parameters and variables, their values can be passed to parameters, even same-named ones. The example does this for parameter $para_1.

Although activating an imported rule functionally resembles including its body at the xsl:apply-imports instruction, the caller’s variables and parameters are out of scope and cannot be referenced in the imported rule.

A local default in the activated rule therefore shadows a same-named caller parameter unless its value is explicitly forwarded.

Caution — Difference between XSLT 1.0 and 2.0:
XSLT 1.0 cannot pass values to parameters in imported rules activated by xsl:apply-imports because its content model disallows the xsl:with-param instruction.

Passing values from  xsl:next-match (available from XSLT 2.0):

<xsl:template select="beispiel">
  <xsl:param name="para1" select="defaultwert"/>
  ...
  <xsl:next-match>
    <xsl:with-param name="para1" select="$para1"/>
  </xsl:next-match>
  ...
</xsl:template>

Instead of xsl:apply-imports, XSLT 2.0 can use xsl:next-match to reactivate overridden rules. Unlike xsl:apply-imports, this also considers lower-priority rules, not only lower import precedence. XSLT 1.0 activates only imported rules this way.

Tunnel parameters (from XSLT 2.0):

Values can also pass between rules as tunnel parameters, again using xsl:with-param with an additional tunnel attribute set to "yes". The value is added to the tunnel stream and becomes available both in the called template and its descendants without explicit forwarding. It “tunnels” through intermediate calls.

A tunnel parameter resembles a global, but its value can change when its originating xsl:with-param instruction is instantiated again. A same-named existing tunnel value is overridden during the new parameter’s scope, not deleted: it becomes accessible again when the creating template and its descendants finish.

A tunnel value can be caught only by a xsl:param instruction with both a matching name and a tunnel attribute set to "yes". Reading it along the way does not remove it: descendants can still access it.

An explicitly passed value takes precedence over a tunnel value. Thus a xsl:param with tunnel="yes" accepts a directly passed value even without a tunnel attribute, despite an existing same-named tunnel value. Such implicit duplicates are allowed.

Two same-named parameters in one template are expressly forbidden, even if one has a tunnel attribute. Direct and tunnel values with the same expanded name therefore cannot be used simultaneously.

Parameters in stylesheet functions (from XSLT 2.0):

Arguments to a stylesheet function declared with xsl:function are defined internally by xsl:param elements:

<xsl:function name="bsp:beispiel">
  <xsl:param name="para_1" as="xs:integer"/>
  <xsl:param name="para_2" as="xs:string"/>
  <xsl:param name="para_3" as="xs:string"/>
  ...
  <!-- Funktionsblock mit Referenzen $para_1, $para_2 etc. -->
  ...
  <!-- Ergebnisausgabe mit xsl:result -->
</xsl:function>

No defaults for function parameters:
Unlike normal syntax, the xsl:param declarations in functions may contain neither a select attribute nor a template block. Defaults are forbidden, not merely unavailable. Normally the absence of a select attribute and body implies an empty-string default, but not here. The function above must be called as:

bsp:beispiel(42, 'Douglas Adams', 'Per Anhalter durch die Galaxis')

The number of values supplied (arity) must exactly match the function’s parameter count. Stylesheet functions cannot have variable or optional arguments, for example using required="no", which is forbidden for function parameters. Excess arguments cause an error rather than being discarded, unless a same-named function with the matching arity exists: functions can be overloaded to that extent.

Calls do not use parameter names: argument order alone determines assignment. Names are used only inside the function to reference parameters.

Examples:

Example 1 — Global parameter with a default:

Stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="eingabedoc" select="'default.xml'"/>
  <xsl:variable name="docverarbeitung" select="document($eingabedoc)"/>
  <!-- Template-Regeln etc. -->
</xsl:stylesheet>

Explanation:

The global parameter $eingabedoc has the default 'default.xml' as a URI string, here only a filename. A subsequent global variable uses it as an argument to the document() function, bringing that document into processing.

A caller can supply any URI; otherwise the default document is processed.

Example 2 — Copyright text using parameters:

Stylesheet:

<xsl:template name="copyright">
  <xsl:param name="verlag" select="'Galileo-Computing'"/>
  <xsl:param name="jahr" select="2004"/>
  <p><xsl:text>Copyright: </xsl:text><xsl:value-of select="$verlag"/><xsl:text>, </xsl:text><xsl:value-of select="$jahr"/><xsl:text>. Alle Rechte vorbehalten.</xsl:text></p>
</xsl:template>

Output (default):

<p>Copyright: Galileo-Computing, 2004. Alle Rechte vorbehalten.</p>

Without supplied values, the named template outputs standard text using local defaults. A caller can override either or both defaults:

<xsl:call-template name="copyright">
  <xsl:with-param name="jahr" select="'(o.J.)'"/>
  <xsl:with-param name="verlag" select="'Galileo-Design'"/>
</xsl:call-template>

Passing order does not matter; it is deliberately reversed here. Output is:

<p>Copyright: Galileo-Design, (o.J.). Alle Rechte vorbehalten.</p>

Example 3 — Replacing a substring in an input string:

Stylesheet:

<xsl:template name="substring-ersetzen">
  <!-- Eingabeparameter -->
  <xsl:param name="eingabestring" />
  <xsl:param name="suchstring" />
  <xsl:param name="ersetzungsstring" />
  <xsl:analyze-string regex="{$suchstring}" select="$eingabestring">
    <xsl:matching-substring>
      <xsl:value-of select="$ersetzungsstring"/>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

This named template replaces every occurrence of a substring ($suchstring) in an input string ($eingabestring) with a replacement string ($ersetzungsstring). The input, search and replacement strings are passed to corresponding local parameters at the call.

Element definition:

XSLT 1.0:

<!-- Category: top-level-element -->
<xsl:param 
     name = qname 
     select = expression>

     <!-- Content: template -->
</xsl:param>

XSLT 2.0:

<!-- Category: declaration -->
<xsl:param
     name = qname
     select? = expression
     as? = sequence-type 
     required? = "yes" | "no"
     tunnel? = "yes" | "no" >

     <!-- Content: sequence-constructor -->
</xsl:param>

   

  

back to top