[xquery-talk] How to handle empty sequence?

Michael Kay mhk at mhk.me.uk
Tue Jan 18 09:32:22 PST 2005

>             let $version := ($release3/version)
>             return
>               <td>
>               <CENTER>
>               {
>                if (string-length(string($version/node())) = 
> 0) then "NA"
>                else $version/node()
>                }
>             </CENTER>
>             </td>

> This query works fine if all applications have a version in each
> environment. The problem is when I have an application that 
> does not have
> any versions released to a particular env. .When this occurs, the
> $sorted_releases sequence will be empty, so I never get to 
> "for $release3"
> return to generate the necessary table element (which would be blank).

Yes, you can't do $version/node() if $version is a string. But why are you
doing $version/node() anyway? If the input is <version>13<!--I
think--></version>, this will fail because the version element has two child
nodes, and string() must take a singleton as its argument. Better to take
the string value of the element node itself.

I would do

let $version = string(($release3/version, "NA")[1])

More information about the talk mailing list