> let $subj := "administrative"
> let $title :="AC"
> let $op :="AND"
> let
> $q1 :=
>    if (string-length($subj) and $op ='AND' )
>       then concat ("cts:contains(subject,   
>       cts:and-query(cts:word-query('",
>       $subj, "', 'case-insensitive'))) and ")
>    else (),
> $q2 :=
>    if (string-length($subj) and $op ='AND' )
>       then concat ("cts:contains(title, 
>       cts:and-query(cts:word-query('",
>       $title, "', 'case-insensitive'))) and ")
>    else (),
> $q3 :=fn:replace(concat ($q1, $q2), "and $", "")
> 
> return (<P>{:=/records/record[$q3]}</P>)

in xquery (at least, in marklogic's implementation) one can write
functions that return functions. for example:

define function get ($element, $term) {
cts:element-query(xs:QName($element), cts:word-query($term, 'case-insensitive'))
}

if $element here is "subject" and $term is "administrative", what the
function returns is 

cts:element-query(xs:QName("subject"),
cts:word-query("administrative", 'case-insensitive'))

in other words, the get function returns a function to search subject
elements for the string "administrative". this means i can assign that
function to a variable and use it downstream, and i can do it multiple
times. so i can do something like this (if searching a Dublin Core
record, for example, based on user input via a web page):

cts:search(//record, cts:and-query(($subject, $title)))

where i've previously assigned $subject and $title to the return
values of the get command, e.g.,

let $subject := if ($subject_term = "") then () else get("subject", 
$subject_term)
let $title := if ($title_term = "") then () else get("title", $title_term)

the prefix notation (effectively, and(a, b, c)) means that i can "and"
(or "or") an arbitrary number of these together in a sequence, and if
it turns out that some of the user input was empty (in Dublin Core
terms, subject, title, but no creator, say), and'ing a sequence that
includes the empty sequence doesn't affect the result.
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to