xquery help

Questions about XML that are not covered by the other forums should go here.
prusu83
Posts: 1
Joined: Mon Nov 07, 2005 6:42 pm

xquery help

Post by prusu83 » Mon Nov 07, 2005 6:43 pm

In the xquery code bellow, i can't seem to get the syntax arranged starting with this line if (empty($bb)) then . The problem is either syntax error or i have xquery code shown in the output xml. I would appreciate help with the syntax of those last lines - how to place the curly brackets.


for $x in doc ($doc_name)/ProcessValueTrace/Trace
return
<Trace>
{

for $y in $x/DtsResult
let $bb:=for $xx in $y/DtsRequest where (contains($xx/PDU, string($pdu_byte_pattern))) return $xx
return
(: my problem is from here down. i have 3 outputs - first the beginning tag and a few tags i just output :)
if (empty($bb)) then
""
else
return
<DtsResult>
{ $y/LogicalLink }
{ $y/ComPrimitive }

(: than i output some selected tags:)
for $z in $y/DtsRequest
where (contains($z/PDU, string($pdu_byte_pattern)))
return
$z

(: than i output the close tag - in case i had something before it:)
if (empty($bb)) then
""
else
</DtsResult>

</Trace>

I tried to place each of the 3 outputs in {..} but i kept getting error. I played a little with the {} but i didn't get them and the output right. I appreciate your help.

Best regards.
Paul

dsewell
Posts: 115
Joined: Mon Jun 09, 2003 6:02 pm
Location: Charlottesville, Virginia USA

Post by dsewell » Tue Nov 08, 2005 4:51 am

Code: Select all

 if (empty($bb)) then
""
else
return
<DtsResult>
The problem is your "else" followed by "return". The XQuery syntax for if/then is
"if (condition) then XQueryStatement else XQueryStatement". For example

Code: Select all

if ( true() )
then "yes"
else "no"
which produces the output string "no". But the following will give a syntax error:

Code: Select all

if ( true() )
then "yes"
else return "no"
The best way to remember this is that "return" in XQuery never stands alone. It is always part of a FLOWR construct. It must have at least one "let" or "for" statement preceding it.

Post Reply