Re: [xsl] Checking if a variable has a value

Subject: Re: [xsl] Checking if a variable has a value
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 31 Aug 2022 08:10:56 -0000
A variable always has a value.

If a parameter is declared as `<xsl:param name="git-hash"/>`, with no `select`
or `as` attribute, then its default value (if you don't supply one) is a
zero-length string. There is no way of distinguishing this from the case where
the user supplied a zero-length string explicitly. If you need to distinguish,
then select a different default (using the `select` attribute).

You can test for a non-zero-length string using `test="$git-hash"`. Or if you
prefer,  `test="$git-hash != ''"`.

> On 31 Aug 2022, at 02:27, Mark Giffin m1879@xxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> XSLT 2.0, Saxon processor.
>
> What's a good way to check if a variable has a value? I want to print
something if a variable has a value, and not print it if it is not given a
value. This must be extremely simple but I don't see it.
>
> Variable is declared:
> <xsl:param name="git-hash"/>
>
> This will sometimes be given a string value like this:
> 26166cf7f809f5107778c2742e06edb024c5ffef
>
> I tried this and others:
> <xsl:if test="matches($git-hash, '\d|[a-z]')">
>     <dc:subject>git hash: <xsl:value-of select="$git-hash"/></dc:subject>
> </xsl:if>
>
> When the variable has no value, the following is output instead of nothing:
> git hash: ${git-hash}

I can't see any way that would be output by any of your code samples.
>
> The following works but only if the value has numbers in it:
> <xsl:if test="matches($git-hash, '\d')">
>
> None of these work, what have I missed:
> <xsl:if test="$git-hash">
> <xsl:if test="boolean($git-hash)">
> <xsl:if test="matches($git-hash, '\d|\S')">
> <xsl:if test="matches($git-hash, '\d|[a-z]')">
>
> <xsl:choose>
>    <xsl:when test="not($git-hash)"></xsl:when>
>    <xsl:otherwise>
>        <dc:subject>git hash: <xsl:value-of
select="$git-hash"/></dc:subject>
>    </xsl:otherwise>
> </xsl:choose>
>
> Thanks,
> Mark
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

Current Thread