[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

XQuery quirk of the day

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


  XQuery quirk of the day
At 2012-04-13 19:09 +0000, David Lee wrote:
>Content-Language: en-US
>Content-Type: multipart/alternative;
> 
>boundary="_000_220256FDE771B74FB662165BC562CF592267FBBFCH1PRD0802MB107_"
>
>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
T:\ftemp>

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().

>or
>     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:http://x-query.com/mailman/listinfo/talk
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.