XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / with-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
Instruction xsl:with-param passes a value to an xsl:param instruction in a template rule invoked in the same context.
Classification | Subinstruction |
Function group | Parameter passing |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted content:
xsl:with-param occurs as a subinstruction inside xsl:apply-templates and xsl:call-template. Since XSLT 2.0, it may also occur in xsl:apply-imports, explicitly forbidden in XSLT 1.0, and the new instruction xsl:next-match.
An optional sequence constructor builds a temporary tree passed to the target parameter. The instruction must instead be empty when attribute select is present; the passed value then comes from its XPath expression in attribute select.
With neither a constructor nor attribute select, the empty sequence is passed.
Attributes:
Standard attributes apply. In addition, xsl:with-param has three optional attributes, select, as and tunnel, plus required attribute name.
as
Value | sequence-type |
Usage | optional |
Introduction | XSLT 2.0 |
The as attribute specifies an XPath 2.0 sequence type for the parameter value. With attribute as, the value must conform to that type.
Otherwise conversion is attempted; failure is a type error (ERR XTTE0590). Without attribute as, the value from the evaluated select expression or constructor is retained unchanged.
Note: The as attribute is unavailable in XSLT 1.0.
name
Value | qname |
Usage | required |
Introduction | XSLT 2.0 |
A string in QName form, identifying the xsl:param instruction receiving the value. Names are freely chosen, but inside xsl:call-template a missing same-named target is an error, introduced in XSLT 2.0.
Same-named variables and parameters may be in scope: xsl:with-param invokes parameters rather than declaring them. Recursive template rules may even need this overlap.
select
Value | xpath-expression |
Usage | optional |
Introduction | XSLT 1.0 |
An XPath expression evaluated in the current context determines the passed value. With attribute select, the instruction cannot contain a constructor.
tunnel
Value | "yes" | "no" |
Usage | optional |
Introduction | XSLT 2.0 |
The tunnel attribute adds the value to the tunnel stream for the called template and its direct or indirect descendants. The default is "no", so normally it is not added.
With tunnel="yes", all same-named xsl:param instructions down the call chain can access it if their attribute tunnel is also tunnel="yes".
Note: The tunnel attribute is unavailable in XSLT 1.0.
Purpose:
Instruction xsl:with-param passes a value when a rule is activated, storing it in local parameter xsl:param and overriding its default. The target’s QName appears in instruction xsl:with-param through attribute name.
Determining the passed value:
The instruction produces a temporary tree, or simple string, passed to the target. Value creation resembles xsl:variable or xsl:param, using the surrounding calling rule’s context.
There are two sources:
Optional attribute
select.An optional contained sequence constructor.
Use either attribute select or content, never both.
Neither is also permitted, though rarely useful. Without attribute select and with an empty instruction, the empty sequence is passed, or an empty string in XSLT 1.0, overriding the target default.
Invocation context:
Instruction xsl:with-param is used inside template-invocation instructions. XSLT 1.0 allows xsl:apply-templates and xsl:call-template; XSLT 2.0 adds xsl:apply-imports and xsl:next-match. Usage is:
<xsl:apply-templates>
<xsl:with-param name="param1" select="$wert1"/>
<xsl:with-param name="param2" select="$wert2"/>
<!-- weitere Parameterübergaben: -->
...
</xsl:apply-templates>
Any invocation instruction above may replace the illustrated xsl:apply-templates. Any number of xsl:with-param instructions may occur in any order, independently of declaration order. QNames alone determine the mapping.
Two xsl:with-param instructions in one call must not share a QName, even if one has attribute tunnel set to tunnel="yes" and the other does not.
If no target parameter exists, the value is discarded and instruction xsl:with-param is ignored.
The exception is xsl:call-template, requiring a corresponding target or rejecting the stylesheet with static error ERR XTSE0680. Existing parameters need not receive values unless attribute required equals "yes", since otherwise its default applies.
Note: This stricter behavior is new in XSLT 2.0. In 1.0, unsuccessful parameter passing in xsl:call-template is ignored like in xsl:apply-templates.
Tunnel parameters
Tunnel parameters also pass values between rules. They use xsl:with-param with additional attribute tunnel set to tunnel="yes". The value joins the tunnel stream and is accessible to the called template and descendants without explicit forwarding, tunneling through intermediate calls.
A tunnel parameter resembles a global one, but can receive a new value when its creating xsl:with-param instruction is instantiated again. A same-named stream value is temporarily overridden, not deleted, and reappears after the new template and descendants finish.
Only an instruction xsl:param with matching name and attribute tunnel set to "yes" can capture it. Reading does not remove it from the stream; descendants still have access.
Explicit values take precedence over tunnel values. An xsl:param with tunnel="yes" accepts an explicitly passed value, even without attribute tunnel, despite a same-named tunneled value; such implicit duplicates are permitted.
Templates cannot declare two same-named parameters, even if one has attribute tunnel. Direct and tunneled values with identical expanded names therefore cannot be used simultaneously.
Examples:
Example 1 – passing a value from xsl:call-template:
<xsl:call-template name="textformatierung">
<xsl:with-param name="trennstring"> *** </xsl:with-param>
</xsl:call-template>
<xsl:template name="textformatierung">
<xsl:param name="trennstring"> – - – </xsl:param>
...
</xsl:template>
Here, xsl:with-param supplies a local parameter in a named template, overriding its default.
Example 2 – static error with xsl:call-template:
<xsl:call-template name="beispiel">
<xsl:with-param name="hallo">Hallo Template</xsl:with-param>
</xsl:call-template>
<xsl:template name="beispiel">
<xsl:param name="benoetigter_wert" required="yes"/>
...
</xsl:template>
This causes two static errors in XSLT 2.0: a value passed through xsl:call-template for parameter $hallo is absent in the target, while another required parameter marked required="yes" named $benoetigter_wert is not passed.
Note: This is irrelevant in XSLT 1.0: xsl:call-template may pass to a nonexistent target, and xsl:param has no attribute required yet.
Example 3 – xsl:with-param in xsl:call-template:
<xsl:template name="listenausgabe">
<!-- Default, falls kein Wert übergeben wird: -->
<xsl:param name="format">1. </xsl:param>
<div>
<xsl:number format="{$format}"/>
<xsl:apply-templates />
</div>
</xsl:template>
<!-- für einfache Listen: -->
<xsl:template match="liste/listenelement">
<xsl:call-template name="listenausgabe"/>
</xsl:template>
<!-- für verschachtelte Listen: -->
<xsl:template match="liste//liste/listenelement">
<xsl:call-template name="listenausgabe">
<xsl:with-param name="format">a. </xsl:with-param>
</xsl:call-template>
</xsl:template>
A named template formats a list, receiving a parameter through xsl:with-param according to nesting depth. Without it, the default declared in xsl:param, namely "1. ", applies.
Example 4 – xsl:with-param in xsl:apply-templates:
<xsl:template match="kapitel" mode="rot">
<xsl:apply-templates select="abschnitt">
<xsl:with-param name="farbe">red</xsl:with-param>
</xsl:apply-templates>
</xsl:template>
<!-- bei direktem Aufruf bleibt die Schrift schwarz -->
<xsl:template match="abschnitt">
<xsl:param name="farbe">black</xsl:param>
<p style="color:{$farbe}"><xsl:apply-templates/></p>
</xsl:template>
Here, xsl:with-param occurs inside xsl:apply-templates. A normal call to template abschnitt uses the default color. Processing a chapter in mode "rot" activates the upper rule, which uses xsl:apply-templates to form a node sequence for template abschnitt while passing a parameter overriding its local color.
Note that mode "rot" is not passed on; only default templates do this automatically, see xsl:template. This would require xsl:apply-templates to have attribute mode too.
Element definition:
XSLT 1.0:
<!-- Category: sub-instruction -->
<xsl:with-param
name = qname
select = expression>
<!-- Content: template -->
</xsl:with-param>
XSLT 2.0:
<!-- Category: sub-instruction -->
<xsl:with-param
name = qname
select? = expression
as? = sequence-type
tunnel? = "yes" | "no">
<!-- Content: sequence-constructor -->
</xsl:with-param>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn