N. America: 800 876 3101 | World: 44 (0) 1753 218 930

How can I return the result of multiple expressions?

I have the following XQuery:

<root>
  {
    for $a in (1 to 10)
    return
        <number>{ $a }</number>
        <number-plus-100>{ $a + 100 }</number-plus-100>
  }
</root>

But the syntax coloring is "broken," and executing this XQuery returns this error:

[DataDirect][XQuery][err:XPST0003]Error at line 6, column 19. Expected "}", but encountered ">".

If you want to return multiple expressions (the <number> and <number-plus-100> elements in this case), you need to return a sequence containing multiple expressions; to do that, you use the comma operator (http://www.w3.org/TR/xquery/#dt-comma-operator). NOTE: the comma operator is the operator with the lowest precedence in XQuery; so writing something like:

    …

    return
        <number>{ $a }</number>,
        <number-plus-100>{ $a + 100 }</number-plus-100>

... will trigger another error:

[DataDirect][XQuery][err:XPST0008] Undeclared variable $a;

Instead you need to change the XQuery into:

<root>
  {
    for $a in (1 to 10)
    return (
        <number>{ $a }</number>,
        <number-plus-100>{ $a + 100 }</number-plus-100>
    )
  }
</root>


Next Question!

Why does my XQuery fail when I override the default namespace?

Submit Your DataDirect XQuery Tip or Trick

Tell us your XQuery Tip or Trick – if it gets published on our site, you’ll receive a

$10.00 Amazon.com
Gift Certificate!

Submit your tip or trick today.