Re: [xsl] RE: Variable: true or false

Subject: Re: [xsl] RE: Variable: true or false
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 10 Apr 2001 18:00:58 +0100
Hi Tom,

> As a follow on I was wondering what is supposed to happen if I change:
>
>   <xsl:variable name="selectUser"/>
> to:
>   <xsl:param name="selectUser"/>
>
> I want to determine if a param passed into a stylesheet has been
> assigned a value. In a perfect world, should I still be able to do
> the simple true/false test? My guess from looking at the spec is
> that "selectUser" should be an empty string if it is not assigned,
> just like the variable case.

That's right.  xsl:param and xsl:variable work in a very similar way,
it's only that xsl:params can have values assigned to them from
'outside' whereas xsl:variables can't.

There isn't a way to check whether a parameter has been passed a
value, but you can usually get around it by making the default value
of the parameter something that it will never be.  For example:

  <xsl:param name="selectUser" select="'a-random-string'" />

If you set up this default, then you can test whether the $selectUser
parameter has that value:

  <xsl:if test="$selectUser = 'a-random-string'">
     ...
  </xsl:if>

Of course if you're writing a utiltiy template, often it's possible
for people to pass in parameter values that are just plain illegal,
and you need to check for these illegal values and do something
appropriate anyway.  You might find it more convenient to share this
processing, and set the default to an illegal value.

If you only want to send a message (and possibly terminate the
transformation) if the parameter isn't specified, then another method
is to use the xsl:message element in the content of the xsl:param:

  <xsl:param name="selectUser">
     <xsl:message terminate="yes">
        This template must be passed a value for the 'selectUser'
        parameter.
     </xsl:message>
  </xsl:param>

[It's not clear to me where in the spec it says that the default value
of the parameter will not be evaluated if the template is passed a
parameter value, but it seems sensible and Saxon and MSXML do it.]
  
I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread