1

I am trying to run XQuery code, but variable in return expression is not being resolved

When I run the following XQuery code

for $x in (<Person/>,<Person/>)
return $x

I get the output

<Person/>
<Person/>

Which is fine, but when I run the following code

for $x in (<Person/>,<Person/>)
return <Name>$x</Name>

I get

<?xml version="1.0" encoding="UTF-8"?>
<Name>$x</Name>
<Name>$x</Name>

Why is the variable $x not resolved when enclosed inside an element in a return expression?

dirkk
  • 6,160
  • 5
  • 33
  • 51
ammar26
  • 1,584
  • 4
  • 21
  • 41

1 Answers1

3

You should place $x in curly braces, XQuery needs to know that you don't want the literal text.

for $x in (<Person/>,<Person/>)
return <Name>{$x}</Name>
Ewout Graswinckel
  • 1,263
  • 6
  • 9
  • Thanks, Can you explain why in the first case X-Query didn't resolved it as literal text ? – ammar26 Jul 25 '14 at 15:22
  • 1
    I guess it's just a xquery design decision. I imagine it would also have been possible to have any unescaped content be interpreted as an xquery expression. But then you would have to do something special if you want to output literal text. – Ewout Graswinckel Jul 25 '14 at 15:33