[xquery-talk] Elegant form of testing for () and ''
Michael Kay
mike at saxonica.com
Fri Mar 4 21:46:04 PST 2011
> 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/20110304/820ae3d5/attachment.htm
More information about the talk
mailing list