This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: forwarding only existing parameter


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()"/>
   ...
</xsl:template>

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>

or

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

Regards,


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


[1] http://www.w3.org/TR/REC-xslt-19991116.html#variable-values
[2] http://www.w3.org/TR/REC-xslt-19991116.html#section-Conditional-Processing-with-xsl:if

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]