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

Subject: Re: [xsl] Checking if a variable has a value
From: "Mark Giffin m1879@xxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 31 Aug 2022 05:22:24 -0000
Thanks Syd and Graydon, I tried all of these and none can detect when
$git-hash is empty, to avoid firing the xsl:if. That's too weird. It must be
something else. If it makes any difference, the $git-hash variable is not
defined in the stylesheet I am using it in, it is defined in another file that
imports the stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:fo="http://www.w3.org/1999/XSL/Format"; version="2.0">
    [...]
    <xsl:param name="git-hash"/>
    [...]
    <xsl:import href="metadata.xsl"/>
</xsl:stylesheet>

I use $git-hash in metadata.xsl. It does work if it has a value. This is
inside the DITA Open Toolkit. I can get by without fixing this but it bothers
me.

Thanks,
Mark

-----Original Message-----
From: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Aug 30, 2022 6:44 PM
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] Checking if a variable has a value

On Wed, Aug 31, 2022 at 01:27:20AM -0000, Mark Giffin m1879@xxxxxxxxxxxxx
scripsit:
> XSLT 2.0, Saxon processor.
>
> What's a good way to check if a variable has a value?

> I tried this and others:
>
> git hash:
>


git hash:


would be customary.

string-length($git-hash)

(since 0 is false)

matches($git-hash,'\P{Zs}')

("any character EXCEPT a member of the unicode character category "Separator,
space")

exists($git-hash)

returns true if the sequence isn't empty, and a variable with one thing
in it is a sequence of length one.

Would also all work.

(So would head($git-hash) but I don't think fn:head() is available in
2.)

I would generally want to go with exists(), at least if you're sure
you're getting actual nothing in the param when you're not getting the
hash value. Typing parameters is generally helpful with avoiding that
kind of surprise but not so much when it might be one space instead of
the expected hash value, since they'd both be strings. (Type the param
anyway, just in case.)

--
Graydon Saunders | graydonish@xxxxxxxxx
&THORN;&aelig;s ofer&eacute;ode, &eth;isses sw&aacute; m&aelig;g.
-- Deor ("That passed, so may this.")

Current Thread