[xquery-talk] if statements within where clauses?

Jonathan Robie jonathan.robie at datadirect.com
Wed Jun 14 16:21:59 PDT 2006


Use parentheses around the ifs. Here's an expression that works:

declare variable $doc := document{ 
<tei.2><article><author><first>Cindy</first></author></article></tei.2> };

for $a in $doc//author
where (if ($a/first)
         then $a/first eq 'Cindy'
         else true())
  and (if ($a/last)
         then $a/last eq 'Girard'
         else true())
return $a

Does that help?

Jonathan

P.S., of course my example isn't valid TEI ;->

-- 
Read my Blog: http://blogs.datadirect.com/jonathan_robie/
Learn XQuery: http://media.datadirect.com/download/docs/ddxquery/tutorial_query.html
Learn XQJ (the JDBC for XQuery): http://www.datadirect.com/developer/xquery/topics/xqj_tutorial/
Get DataDirect XQuery: http://www.datadirect.com/products/xquery/



Cindy Girard wrote:
> Thanks, Jonathan.
>
> I switched it to this:
>
> $hits :=
>   for $entries in collection($collctn)//tei.2
>     let $docname := concat($entries/@id, '.xml')
>     let $divs := $entries//div1
>     let $text := $entries/text
>     where contains($text, $keyword)
>       and if ($region = 'all') then ()
>         else $entries//classcode = $region
>       and if ($gender = 'all') then ()
>         else $entries//classcode = $gender
>       and if ($race = 'all') then ()
>         else $entries//classcode = $race
>       and if ($type = 'all') then ()
>         else $entries/@type = $type
>                                                    
>   return
>     <doc>{$entries/@type, <ct>-</ct>}</doc>,
>
> But I get the following error:
>
> Error: Error in XQuery expression: syntax error, unexpected "if" [err:XPST0003],
>
> What else can I try?
>
> Thanks,
> Cindy
>
>
> JR> Jonathan Robie wrote:
>   
>>> Hi Cindy,
>>>
>>> XQuery allows expressions to be combined very freely, and you can 
>>> certainly use an if expression for conditional logic in a WHERE 
>>> clause, as follows:
>>>
>>>    ....
>>>    where contains($text, $keyword)
>>>      and if ($var ='all')
>>>             $entries//classcode = $region
>>>          else
>>>             true()
>>>
>>> I did not understand what you are trying to do well enough to know 
>>> exactly what the query should look like.
>>>
>>> Hope this helps!
>>>
>>> Jonathan
>>>
>>>       
>
> JR> _______________________________________________
> JR> talk at xquery.com
> JR> http://xquery.com/mailman/listinfo/talk
>
>
>
>   



More information about the talk mailing list