RE: How can you tell if a variable exists?

Subject: RE: How can you tell if a variable exists?
From: Kay Michael <Michael.Kay@xxxxxxx>
Date: Tue, 2 Nov 1999 09:41:01 -0000
> How can you tell if a variable exists (or rather does not 
> exist), without erroring out of the stylesheet.
> 
> For example:
>   I am using the SAXON servlet to present HTML to the user.  
> I am passing in the parameters from doPost to the stylesheet.  If the 
> parameters exist, all is well.  But when a parameter does not exist, and
is not 
> passed to the stylesheet, I just get an exception.  How do I prevent that?
> 
Referring to $v when there is no <xsl:param name="v"> or <xsl:variable
name="v"> in scope is an error, and you can't trap this. But referring to $v
when there is a declaration of <xsl:param name="v"> should not give an
exception, whether or not the parameter has been given a value.

>From the description of your problem, I would declare a global parameter

<xsl:param name="v"/>

and then in your code you can do

<xsl:choose>
<xsl:when test="not($v)"> <!-- parameter has not been supplied -->
</xsl:when>
<xsl:otherwise> <!--parameter has been supplied --> </xsl:otherwise>
</xsl:choose>

The reason this works is that when no actual parameter v is supplied, it
takes its default value, which in this case is an empty string (because the
xsl:param has no select expression or content), and the test not($v) returns
true if $v is an empty string.

Mike Kay


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


Current Thread