RE: [xsl] XSLT 2.0 empty string

Subject: RE: [xsl] XSLT 2.0 empty string
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 28 Feb 2005 14:00:53 -0000
There's actually nothing specific to XSLT 2.0 here: these are all 1.0
expressions where 2.0 gives the same result as 1.0

As with XPath 1.0, when you compare a set of things to a single thing, the
result is true if there is at least one match. This is true whichever
operator you use. This means that if the set of things is empty, the result
of the comparison is false, whether the operator is "=", "!=", or anything
else.

So if $f is an empty sequence and $s is a string, 

  $f = $s is always false
  $f != $s is always false

You can test whether a sequence is empty using the function empty($f).

The result of string($f) when $f is an empty sequence is the zero-length
string, "". 

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Andrew Welch [mailto:ajwelch@xxxxxxxxxxxxxxx] 
> Sent: 28 February 2005 12:14
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] XSLT 2.0 empty string
> 
> 
> Consider the following code:
> 
> <xsl:variable name="foo" select="nothing" as="xs:string?"/>
> 	
> <xsl:choose>
> 	<xsl:when test="$foo != ''">A</xsl:when>
> 	<xsl:when test="$foo = ''">B</xsl:when>
> 	<xsl:when test="not($foo != '')">C</xsl:when>
> </xsl:choose>
> 
> When there isn't a <nothing> element, the output is C.  That is:
> 
> $foo != '' is false
> 
> and
> 
> $foo = '' also is false 
> 
> Which is strange.  If I do "$foo is empty" then Saxon tells 
> me $foo is a
> string and not a nodeset.  After adding the explicit cast, the test
> passes:
> 
> string($foo) = '' 
> 
> Which suggests that $foo isn't a string (so which is it?).  
> It almost as
> if the empty nodeset doesn't get implicitly cast like a 'populated'
> nodeset, and the as: attribute is ignored.  Is there a difference
> between the way the two are handled?  
> 
> Also, is using "!= ''" a bad way of checking if the variable 
> has content
> when the variable type is 'xs:string?' (ie optional)?
> 
> thanks
> andrew

Current Thread