-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.xql
141 lines (126 loc) · 5.07 KB
/
query.xql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(:
: Copyright (C) 2017 Wolfgang Meier
:
: This program is free software: you can redistribute it and/or modify
: it under the terms of the GNU General Public License as published by
: the Free Software Foundation, either version 3 of the License, or
: (at your option) any later version.
:
: This program is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
: GNU General Public License for more details.
:
: You should have received a copy of the GNU General Public License
: along with this program. If not, see <http://www.gnu.org/licenses/>.
:)
xquery version "3.1";
module namespace query="http://www.tei-c.org/tei-simple/query";
import module namespace tei-query="http://www.tei-c.org/tei-simple/query/tei" at "query-tei.xql";
import module namespace docbook-query="http://www.tei-c.org/tei-simple/query/docbook" at "query-db.xql";
import module namespace jats-query="http://www.tei-c.org/tei-simple/query/jats" at "query-jats.xql";
import module namespace config="http://www.tei-c.org/tei-simple/config" at "config.xqm";
import module namespace nav="http://www.tei-c.org/tei-simple/navigation" at "navigation.xql";
declare variable $query:QUERY_OPTIONS := map {
"leading-wildcard": "yes",
"filter-rewrite": "yes"
};
declare function query:field-prefix($elem as element()) {
switch (namespace-uri($elem))
case "http://www.tei-c.org/ns/1.0" return
""
case "http://docbook.org/ns/docbook" return
"dbk."
default return
"jats."
};
declare %private function query:sort($items as element()*, $sortBy as xs:string?) {
let $items :=
if (exists($config:data-exclude)) then
$items except $config:data-exclude
else
$items
return
if ($sortBy) then
nav:sort($sortBy, $items)
else
$items
};
declare %private function query:dispatch($config as map(*), $function as xs:string, $args as array(*)) {
let $fn := function-lookup(xs:QName($config?type || "-query:" || $function), array:size($args))
return
if (exists($fn)) then
apply($fn, $args)
else
()
};
(:~
: Query the data set.
:
: @param $fields a sequence of field names describing the type of content to query,
: e.g. "heading" or "text"
: @param $query the query string
: @param $target-texts a sequence of identifiers for texts to query. May be empty.
:)
declare function query:query-default($fields as xs:string+, $query as xs:string,
$target-texts as xs:string*, $sortBy as xs:string*) {
tei-query:query-default($fields, $query, $target-texts, $sortBy),
docbook-query:query-default($fields, $query, $target-texts, $sortBy),
jats-query:query-default($fields, $query, $target-texts, $sortBy)
};
declare function query:options($sortBy as xs:string*) {
query:options($sortBy, ())
};
declare function query:options($sortBy as xs:string*, $field as xs:string?) {
map:merge((
$query:QUERY_OPTIONS,
map {
"facets":
map:merge((
for $param in request:get-parameter-names()[starts-with(., 'facet-')]
let $dimension := substring-after($param, 'facet-')
return
map {
$dimension: request:get-parameter($param, ())
}
))
},
if ($sortBy) then
map { "fields": ($sortBy, $config:default-fields, $field) }
else
map { "fields": ($config:default-fields, $field) }
))
};
declare function query:query-metadata($root as xs:string?, $field as xs:string?, $query as xs:string?, $sort as xs:string) {
let $results := (
tei-query:query-metadata($root, $field, $query, $sort) |
docbook-query:query-metadata($root, $field, $query, $sort) |
jats-query:query-metadata($root, $field, $query, $sort)
)
let $mode :=
if ((empty($query) or $query = '') and empty(request:get-parameter-names()[starts-with(., 'facet-')])) then
"browse"
else
"search"
return map {
"all": $results,
"mode": $mode
}
};
declare function query:get-parent-section($config as map(*), $node as node()) {
query:dispatch($config, "get-parent-section", [$node])
};
declare function query:get-breadcrumbs($config as map(*), $hit as node(), $parent-id as xs:string) {
query:dispatch($config, "get-breadcrumbs", [$config, $hit, $parent-id])
};
declare function query:expand($config as map(*), $data as node()) {
query:dispatch($config, "expand", [$data])
};
declare function query:get-current($config as map(*), $div as node()?) {
query:dispatch($config, "get-current", [$config, $div])
};
declare function query:autocomplete($doc as xs:string?, $fields as xs:string+, $q as xs:string) {
tei-query:autocomplete($doc, $fields, $q),
docbook-query:autocomplete($doc, $fields, $q),
jats-query:autocomplete($doc, $fields, $q)
};