xquery version "1.0"; declare option exist:serialize "expand-xincludes=no"; declare namespace session="http://exist-db.org/xquery/session"; declare namespace request="http://exist-db.org/xquery/request"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace i18n="http://apache.org/cocoon/i18n/2.1"; import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm"; import module namespace buildquery="http://www.risten.no/shared/xqueryfactory" at "../xquery/xqueryfactory.xqm"; (: Create filter expression from query term :) declare function local:filter-expr($term as xs:string) as xs:string { let $class := request:get-parameter("class", "") let $c := if ($class = 'all') then "" else if (contains($class, '0') ) then if (contains($class, 'N') ) then concat(".//@botm &= '", $class, "'" ) else concat(".//@mid &= '", $class, "'" ) else concat(".//@top = '", $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) }; (: Assemble the query string :) declare function local:build-query($term as xs:string, $doc as xs:string) as xs:string { let $expr := local:filter-expr($term), $t := session:set-attribute("query", $expr) return concat("for $r in document('", $doc, "')//entry[", $expr, "] return $r") }; (: Present an overview of query results :) declare function local:displayHitlist($hits as node()+) as element() { let $count := count($hits), $lang := request:get-parameter("showlang", "all"), $collID:= session:get-attribute("collection") return { $hits } }; (: Main function - retrieves some request and session attributes, forks further processing to other defined functions depending on the retrieved values :) declare function local:main() as element()+ { let $term := request:get-parameter("term", ""), $class := request:get-parameter("class", ""), $previous := session:get-attribute("results"), $queryOld := session:get-attribute("query"), $session := session:get-attribute-names(), $request := request:get-parameter-names(), $collID := if ( request:get-parameter("collection", "") ) then request:get-parameter("collection", "") else session:get-attribute("collection") return if ( string-length($term) = 0 and ($class eq 'all' ) ) then if (exists($previous)) then local:displayHitlist($previous) else

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 eq 'all' )) then

Sorry, 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 $doc := ristenutil:get-doc($collID,'center'), $query := local:build-query($term, $doc), $hits := util:eval( $query ), $s := session:set-attribute("results", $hits) return if (empty($hits)) then (

NoHit «{$term}» / «{$class}»!
Document: «{$doc}»
Query: «{$query}»

) else (

UrSrchTrm: «{$term}» / «{$class}»!
Document: «{$doc}»

, local:displayHitlist($hits) ) };