[xquery-talk] if statements within where clauses?

Jonathan Robie jonathan.robie at datadirect.com
Wed Jun 14 15:18:43 PDT 2006

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
I did not understand what you are trying to do well enough to know 
exactly what the query should look like.

Hope this helps!


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:
> Hi.
> I have the following piece of code:
> ----------------------
> $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 $entries//classcode = $region
>       and $entries//classcode = $gender
>       and $entries//classcode = $race
>       and $entries/@type = $type
>     return
>       <doc>{$entries/@type, <ct>-</ct>}</doc>,
> ----------------------
> and I need to make each 'and $entries...' statement conditional
> (if the variable doesn't equal 'all' then it needs to be included,
> otherwise, it needs to be left off).
> I believe I need to use a concat() to do it, but I can't seem to get
> the syntax right.
> Should I put the entire hits variable into a concat()?
> (if it matters, I'm using berkelydbxml for this)
> Thanks,
> Cindy
> Cindy Girard
> clm6u at virginia.edu

More information about the talk mailing list