xquery version "1.0";
(::pragma exist:serialize expand-xincludes=no indent=yes ::)
declare namespace util="http://exist-db.org/xquery/util";
declare namespace fn="http://exist-db.org/local-functions";
declare namespace i18n="http://apache.org/cocoon/i18n/2.1";
(: Create filter expression from query term :)
declare function fn:filter-expr($term as xs:string) as xs:string
{
let $class := request:request-parameter("class", "")
let $c := if ($class = 'all') then ""
else if (contains($class, '-') ) then
concat(".//@botm &= '", substring-after($class, '-'), "'" )
else if ( string-length($class) = 1) then
concat(".//@top &= '", $class, "'" )
else
concat(".//@mid &= '", $class, "'" )
let $f := ".//@href", (: Search the @href for the search term -
it is in the ID of the referenced entry :)
$t := concat("'", $term, "'")
return
if ($term = '*' or $term = '' ) then
$c
else if ( $class = 'all') then
concat($f, " &= ", $t)
else
concat($f, " &= ", $t, " and ", $c)
};
(: Map order parameter to xpath for order by clause :)
(: Since the ordering element is not selectable (in the search form), this function does
not make any sense any more, but I'll keep it for now just in case. :)
declare function fn:order-expr($field as xs:string) as xs:string
{
if ($field = "head") then
"common/head"
else
"common/qa, $r/common/head"
};
(: Assemble the query string :)
declare function fn:build-query($term as xs:string,
$orderby as xs:string)
as xs:string
{
let $expr := fn:filter-expr($term),
$t := request:set-session-attribute("query", $expr),
$docs := "/db/ordbase/terms/SD-terms/termcenter.xml"
return
concat("for $r in document('", $docs, "')//entry[",
$expr, "] return $r")
};
(: Present an overview of query results :)
declare function fn:displayHitlist($hits as node()+)
as element()
{
let $count := count($hits),
$max := request:request-parameter("howmany", "10") cast as xs:int,
$start := request:request-parameter("start", "1") cast as xs:int,
$end := if ($start + $max - 1 < $count) then $start + $max - 1 else $count,
$lang := request:request-parameter("showlang", "all")
return
Your query has timed out, or was too unspecific. Please hit the «Find» button again, or enter a new search query.
else if ($term = "*" and $class = "all") thenSorry, you cannot search for the whole db. Please enter a slightly more specific search - adding one character or specifying a topic is enough.
else let $query := fn:build-query($term, "head"), $hits := util:eval( $query ), $s := request:set-session-attribute("results", $hits) return if (empty($hits)) thenYour document string: {$testdocs}
Your $lang string: {$showlang}
Your session attributes: {$session}
Your request attributes: {$request}
Your class request: {$class}
Your old/short search query: {$queryOld}
Your complete search query:
{$query}