xquery version "3.0"; module namespace ristenxq="http://exist-db.org/apps/restxq/ristenxq"; import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm"; declare namespace exist = "http://exist.sourceforge.net/NS/exist"; declare namespace xmldb="http://exist-db.org/xquery/xmldb"; declare namespace json="http://www.json.org"; declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization"; declare namespace rest="http://exquery.org/ns/restxq"; declare namespace expath="http://expath.org/ns/pkg"; declare namespace repo="http://exist-db.org/xquery/repo"; declare namespace http="http://expath.org/ns/http-client"; (:~ : Simple function to list all dictionaries and return it as XML. :) declare %rest:GET %rest:path("/risten/filter") %rest:query-param("lang", "{$lang}", "nob") %rest:produces("application/json") %output:method("json") function ristenxq:filter-list($lang as xs:string*) { { let $dicts := for $dict in (collection($config:data)//dictmeta, collection($config:data)//termmeta) return $dict for $hit in $dicts//id let $dict-id := $hit/text(), $dict-name := $hit[@xml:lang = $lang]/text() return {$dict-id} {$dict-id} } }; (:~ : Simple function to list all dictionaries and return it as XML. :) declare %rest:GET %rest:path("/risten/dicts") %rest:query-param("lang", "{$lang}", "") %rest:produces("application/xml", "text/xml") function ristenxq:dicts($lang as xs:string*) { { for $dict in collection($config:app-root || "/data/dicts")//id[1] return $dict } }; (:~ : Simple function to list all terminologies and return it as XML. :) declare %rest:GET %rest:path("/risten/terms") %rest:query-param("lang", "{$lang}", "") %rest:produces("application/xml", "text/xml") function ristenxq:terms($lang as xs:string*) { { for $term in collection($config:app-root || "/data/terms")//id[1] return $term } }; (:~ : Search entry. :) declare %rest:GET %rest:path("/risten/search") %rest:query-param("query", "{$query}") %rest:query-param("dict", "{$dict}", "") function ristenxq:query($query as xs:string*, $dict as xs:string*) { { let $queryStr := concat('^', $query) for $hit in (collection($config:data)//l[matches(., $queryStr, "i")], collection($config:data)//head[matches(., $queryStr, "i")]) let $term := string($hit) let $dict := string(root($hit)/child::*[1]/@id) (: if ($dict = null) then let $dict := string(root($hit)/@id) else () :) order by $hit return {$term} {$dict} } }; (:~ : Search entry. :) declare %rest:GET %rest:path("/risten/search-json") %rest:query-param("query", "{$query}") %rest:query-param("dict", "{$dict}", "") %rest:produces("application/json") (:%output:media-type("application/json"):) %output:method("json") function ristenxq:query-json($query as xs:string*, $dict as xs:string*) { ristenxq:query($query, $dict) }; (:~ : Return a subset of the hits. :) (: declare %rest:GET %rest:path("/risten/show-hits") %rest:query-param("start", "{$start}", "1") function ristenxq:show-hits($start as xs:int) { }; :) (:~ : :) declare %rest:GET %rest:path("/risten/article/{$article}") function ristenxq:get-article($article as xs:string*) { collection($config:data)//head[matches(., $article)]/../.. };