[xquery-talk] Elegant form of testing for () and ''

David Lee dlee at calldei.com
Sat Mar 5 07:08:32 PST 2011


Thanks Mike, so obvious in hindsight.

But I had always just considered   

                /path/string()   == string(/path)

 

but its not !  if /path isnt found then the result is () for /path/string()
but not for string(()) 

So obvious !

 

-David

( now time to rewrite ALL my xquery code :)

 

 

----------------------------------------

David A. Lee

 <mailto:dlee at calldei.com> dlee at calldei.com

 <http://www.xmlsh.org> http://www.xmlsh.org

 

From: talk-bounces at x-query.com [mailto:talk-bounces at x-query.com] On Behalf
Of Michael Kay
Sent: Friday, March 04, 2011 4:46 PM
To: talk at x-query.com
Subject: Re: [xquery-talk] Elegant form of testing for () and ''

 

 

I've used this test

 

declare function local:getname( $e ) as xs:string

{

       let $name := $e/name/string()

    return 

if( $name ne '' ) then $name else "<untitled>"

}

 


If you used string($e/name) instead of $e/name/string(), then the result
would be "" (zero-length string) both when name doesn't exist and when name
has the value "". 

You can then either do

if ($name) then $name else "<untitled>"

or the more idiomatic

($name[.], "<untitled>")[1]

Michael Kay
Saxonica

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://x-query.com/pipermail/talk/attachments/20110305/0f91af10/attachment.htm


More information about the talk mailing list