Project

Profile

Help

Detect if optional parameters were provided

Added by Anonymous over 17 years ago

Legacy ID: #4441295 Legacy Poster: Joseph Thomas-Kerr (jak09)

I'm sure I'm missing something simple here, but I can't figure out how to tell whether or not the optional parameter is provided for the template below: <xsl:template name="assignment"> <xsl:param name="name" required="yes"/> <xsl:param name="value" required="no"/> <xsl:variable name="value0"><xsl:value-of select="$value"/></xsl:variable> <xsl:value-of select="$name"/> <xsl:if test="string-length($value0) &gt; 0"> <xsl:text> := </xsl:text> <xsl:value-of select="$value0"/> </xsl:if> <xsl:text>;</xsl:text> </xsl:template> The value could be the empty sequence, a single string (which could be empty, in which case I don't want to output the ":=" operator), or it could be a sequence of strings that together represent some kind of compound expression. using value-of to serialize the parameter is the only way I have come up with to effectively test whether the parameter is present, but it seems rather roundabout. Any suggestions would be welcome. Regards, Joe.


Replies (1)

RE: Detect if optional parameters were provid - Added by Anonymous over 17 years ago

Legacy ID: #4441562 Legacy Poster: Michael Kay (mhkay)

You can't test whether a parameter was supplied, you can only tell whether it is equal to its default value (that is, you can't distinguish the case where the caller omits the parameter from the case where the supply a value that is the same as the default). If there's no "as" attribute and no "select" attribute then the default value is a zero-length string. You can test for the zero-length string using <xsl:if test="$value != ''"> I would encourage you however always to declare the types of your parameters. For example declare this one as <xsl:param name="value" as="xs:string?" select="()" required="no"/> The default value is then an empty sequence, which you can test for as <xsl:if test="exists($value)"> Michael Kay http://www.saxonica.com/

    (1-1/1)

    Please register to reply