Page 1 of 1

XQuery: how to determine scope?

Posted: Tue Aug 02, 2011 5:15 pm
by Zearin
(Wow, am I really the first poster in the XQuery forum?)

So, conceptually I understand XQuery. And I like its philosophy.

(XSL was my first love, though. :wink: XQuery just has a different “feel”, which has a very different mindset when programming. I want to explore that…and if I’m really lucky, start using eXist seriously instead of just screwing around now and then.)

But one thing that has always driven me absolutely crazy is trying to figure out when I’m in “XQuery” scope versus “XML” scope. I can’t figure it out!
  • Sometimes there are curly braces, sometimes there aren’t.
  • When inserting a variable or function call into XML, does it get enclosed in curly braces? If you’re already inside open braces, do you (a) close them, insert the value, then re-open them and continue writing XML, or (b) stick in the $value and keep going?
I have checked tutorials and guides. I really have. I’m obviously missing something right in front of me—something most people have no problem with.

Anybody who can demystify

Re: XQuery: how to determine scope?

Posted: Tue Aug 02, 2011 5:17 pm
by Zearin
Whoops…looks like I forgot to delete a partial thought. Ignore that last sentence fragment. Sorry! Couldn’t seem to find an “edit” button…

Re: XQuery: how to determine scope?

Posted: Tue Aug 02, 2011 5:50 pm
by adrian
Hello,
Zearin wrote:(Wow, am I really the first poster in the XQuery forum?)
The forum is getting reorganized. It's the first time we have an XQuery forum, so most XQuery topics from the past are scattered around. Soon we will gather these topics and move them here.


Well the rule is simple: If you're in XML context(between tags or inside a tag) and want to evaluate an XQuery expression, you have to use curly braces.

Note that you can even have nested braces.
<XML>{
XQuery <XML>{XQuery}</XML> XQuery
}</XML>

e.g.

Code: Select all

<test>{
let $tst := "a test"
return <testing attr="{$tst}">{$tst}</testing>
}</test>
This yields:

Code: Select all

<test>
<testing attr="a test">a test</testing>
</test>
If you're in XML context and you don't use curly braces, the content is considered XML text:

Code: Select all

<test>{
let $tst := "a test"
return <testing attr="$tst">$tst</testing>
}</test>
This yields:

Code: Select all

<test>
<testing attr="$tst">$tst</testing>
</test>
No evaluation of the XQuery code.

Regards,
Adrian