[xquery-talk] XQuery quirk of the day

G. Ken Holman gkholman at CraneSoftwrights.com
Fri Apr 13 12:21:28 PDT 2012

At 2012-04-13 19:09 +0000, David Lee wrote:
>Content-Language: en-US
>Content-Type: multipart/alternative;
>XQuery constantly surprises me !
>Today I was looking for a more concise way of testing if a string is 
>empty or blank.
>The best I came up with is
>      empty($s) or $s eq ''

But empty($s) doesn't return true for an empty string, it returns 
true for an empty sequence.  If $s is defined as an empty string 
you'll get false():

T:\ftemp>type lee.xq
let $s := ""
return empty($s)
T:\ftemp>xquery lee.xq
<?xml version="1.0" encoding="UTF-8"?>false

If you know $s is a string then you can use not($s) to get true() for 
the empty string (and for the empty sequence) because the effective 
Boolean value of an empty string (and an empty sequence) is false().

>     string-length($s) eq 0
>But along the way discovered this
>    if(  () eq () ) then 1 else 2
>    if(  () = () ) then 1 else 2
>Returns 2 !!!
>Of course in hindsight and fine-print its correct, but entirely 
>non-obvious to my eye ...

Just remember that the result is initialized as false() and 
comparisons continue until one of the two operands is the empty set 
or the comparison is true ... which happens right away because both 
operands are the empty set.

I hope this helps.

. . . . . . . . Ken

Public XSLT, XSL-FO, UBL and code list classes in Europe -- May 2012
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/q/
G. Ken Holman                   mailto:gkholman at CraneSoftwrights.com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

More information about the talk mailing list