Re: [xsl] forwarding only existing parameter

Subject: Re: [xsl] forwarding only existing parameter
From: Tony Graham <Tony.Graham@xxxxxxxxxxxxxxx>
Date: Mon, 17 Sep 2001 13:48:15 +0100
Guillaume Rousse wrote at 17 Sep 2001 12:26:13 +0200:
 > I'm trying to forward parameters from a template to another, only if they are 
 > defined, in the most simple way.
 > <template match="foo">
 >   <param name="bar"/>
 >   <apply-templates select=".">
 >     <with-param name="bar" select="$bar"/>
 >   </apply-templates>
 > </template>
 > This one actually forward an empty bar argument when not called with a bar 
 > argument, so it's wrong.

It would be interesting to know why you want to do this, since if you
don't "forward" the parameter, the next template will have to use the
default value declared for the parameter.  In the absence of both
content and a "select" attribute on the <xsl:param> element, the
default value is an empty string [1].  The empty string evaluates as
false when converted to a boolean [2].

If no "bar" parameter is passed to the first template (or, in your
recursive example, the first time the template matching <foo> is used),
then the value of $bar is the empty string, ''.  If you pass the empty
string to the next template as the value of the "bar" parameter, $bar
in the next template has the value of the empty string.  If you don't
pass a value for the "bar" parameter, $bar in the next template will
have the value of the empty string anyway because you're not providing
a default value.  Your choosing to not pass a value for the "bar"
parameter is having no effect whatsoever.

If you need to know whether a value for a parameter was passed to the
template (or stylesheet), use as the default value a value that you
know will not be passed as a parameter value, e.g.:

<xsl:template match="foo">
   <xsl:param name="bar" select="false()"/>

Using false() isn't much different from the empty string case, so if
you're desperate, you could try:

<xsl:template match="foo">
   <xsl:param name="bar" select="1 div 0"/>


<xsl:template match="foo">
   <xsl:param name="bar" select="document('')"/>


Tony Graham
XML Technology Center - Dublin        mailto:tony.graham@xxxxxxxxxxxxxxx
Sun Microsystems Ireland Ltd                       Phone: +353 1 8199708
Hamilton House, East Point Business Park, Dublin 3            x(70)19708


 XSL-List info and archive:

Current Thread