xquery version "1.0"; declare namespace session="http://exist-db.org/xquery/session"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace i18n="http://apache.org/cocoon/i18n/2.1"; declare namespace source="http://apache.org/cocoon/source/1.0"; import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm"; (: Assemble the query string :) declare function local:build-query($lang as xs:string) as xs:string { let $coll := ristenutil:get-collection() return concat("for $r in collection('", $coll, "')//label[@xml:lang eq '", $lang, "'] return {$r/text()}") }; (: Target query: for $r in collection('/db/ordbase/classes/SD-class') //label[@xml:lang eq 'nob'] return {$r/text()} :) (: Present an overview of query results :) declare function local:displayHitlist($hits as node()+, $lang as xs:string) as element()+ { let $collID := session:get-attribute("collection") let $locale := if ($lang eq 'nob') then 'nb' else if ($lang eq 'nor') then 'no' else if ($lang eq 'sme') then 'se' else if ($lang eq 'nno') then 'nn' else if ($lang eq 'swe') then 'sv' else if ($lang eq 'fin') then 'fi' else if ($lang eq 'eng') then 'en' else $lang return if (($locale eq 'no') or ($locale eq 'nb')) then ( context://risten/translations/{$collID}_{$locale}.xml { $hits } , context://risten/translations/{$collID}.xml { $hits } ) else context://risten/translations/{$collID}_{$locale}.xml { $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()+ { (: for $lang in /classmeta[id eq 'SD-class']/langs/lang return {$lang/node()} :) let $collID:= session:get-attribute("collection") return for $lang in /classmeta[id eq $collID]/langs/lang/text() let $query := local:build-query($lang), $hits := util:eval( $query ) return
{ local:displayHitlist($hits, $lang) }
}; (: DEBUG text for the above return clause:

UrSrchTrm: «{$locale}»
And your query: «{$query}»

:)