Subject:Newbie struggling with nested FLWOR expression Author:Jim Salmons Date:05 Sep 2007 08:33 PM Originally Posted: 05 Sep 2007 08:30 PM
My source document is of ITEMS/ITEM where an ITEM has a collection of IMAGES with an IMAGE having a URL. From everything I read, it would appear that the following should work:
<root>
{
let $doc := .
for $ITEM in $doc/ITEMS/ITEM
return
<catnum>{$ITEM/CATNUM/text()}
</catnum>
{for $IMAGE in $doc/ITEMS/ITEM[CATNUM = $ITEM/CATNUM]/IMAGES/IMAGE
return
<url>
{$IMAGE/URL/text()}
</url>
}
}
</root>
However I get a syntax error message saying that a "{" was encountered when a "," or "}" is expected. I must be missing something obvious. Could someone with more experience or understanding please enlighten me?
Subject:Newbie struggling with nested FLWOR expression Author:Minollo I. Date:05 Sep 2007 09:34 PM
Your syntax error can be fixed this way:
<root>
{
let $doc := .
for $ITEM in $doc/ITEMS/ITEM
return (
<catnum>{$ITEM/CATNUM/text()}
</catnum>,
for $IMAGE in $doc/ITEMS/ITEM[CATNUM = $ITEM/CATNUM]/IMAGES/IMAGE
return
<url>
{$IMAGE/URL/text()}
</url>
)
}
</root>
Your nested loop can be written in a simpler way:
<root>
{
let $doc := .
for $ITEM in $doc/ITEMS/ITEM
return (
<catnum>{$ITEM/CATNUM/text()}
</catnum>,
for $IMAGE in $ITEM/IMAGES/IMAGE
return
<url>
{$IMAGE/URL/text()}
</url>
)
}
</root>
Subject:Newbie struggling with nested FLWOR expression Author:Jim Salmons Date:06 Sep 2007 09:57 AM
Excellent, Minollo! Thank you. So simple yet so elusive to a newbie. The on-line examples tend to be the same ones over and over, with explanations that are rather spartan.
Having spent nearly 25-years as a hardcore Smalltalker, and now with so many years of any-language-that'll-do-the-job, mostly procedural scripting languages, I just need to see examples of what's essential and correct to get the hang of this.
I can see, for example, that the 'best practice' method of Smalltalk object design -- where you write lots of little use case-driven, protocol-oriented methods and don't worry much about when/how they are called, only that they do the right thing with their input and return/perform their specific, expected result -- that this mindset will translate easily to the declarative nature of XQuery 'programming'.
Now if I could just learn to be less anal about result formatting in interactive mode (or if there was a 'pretty priniting' button on the Preview pane, I'd be smokin'! :-)
Your simplified version makes perfect sense and will help me a lot as I move forward.
Subject:Newbie struggling with nested FLWOR expression Author:Minollo I. Date:06 Sep 2007 11:37 AM
We understand the pain you are going through, and that's why we have been doing some work recently to put together a list of XQuery tips&tricks from feedback we have received and that we have seen on other discussion lists.
That will soon become public on our http://www.xquery.com website; I'll post a note here when that happens.