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

Subject: Re: [xsl] Checking if a variable has a value
From: "Jean-Paul Rehr rehrjb@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 31 Aug 2022 08:26:26 -0000
>A variable always has a value.

I'm trying to reconcile this statement with Xquery, which I use more
frequently.

In Xquery the following returns false:

let $x := ()
return
      if (exists($x)) then true() else false()

Are you stating that the equivalent cannot happen in XSL variables? I'm
asking this mostly because I've not had the need to explore this until now
and am ignorant about it.

Best,
JPR


On Wed, Aug 31, 2022 at 10:11 AM Michael Kay mike@xxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> 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)
>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/3377836> (by
> email <>)

Current Thread