-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeakers.xq
71 lines (65 loc) · 2.38 KB
/
speakers.xq
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
xquery version "3.1";
(:
: This XQuery extracts from a DraCor play the particDesc data and @who
: attributes assigned to tei:sp elements into a JSON structure that is
: compatible with the one generated by epdracor-whois. It's purpose is to
: reintroduce particDesc related changes that have been made manually into
: the XSLT workflow for epdracor.
:
: Call it like this:
:
: curl http://localhost:8080/exist/rest/db/apps/dracor-v1/speakers.xq?id=ep000148
:)
import module namespace config = "http://dracor.org/ns/exist/v1/config"
at "modules/config.xqm";
declare namespace tei = "http://www.tei-c.org/ns/1.0";
declare namespace ep = "http://earlyprint.org/ns/1.0";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "json";
declare option output:media-type "application/json";
let $col := request:get-parameter("collection", $config:corpora-root)
let $id := request:get-parameter("id", "")
return if (not($id)) then (
response:set-status-code(400),
map {
"error": "missing query parameter 'id'"
}
) else
let $tei := collection($col)//tei:TEI[@xml:id = $id]
let $id := $tei/@xml:id/string()
let $epid := replace(
tokenize(
$tei//tei:sourceDesc/tei:bibl[@type='digitalSource']
/tei:idno[@type='URL' and starts-with(., 'https://texts.earlyprint.org/works/')],
'/'
)[last()],
'.xml',
''
)
return map {
"id": $id,
"epid": $epid,
"particDesc": array {
for $item in $tei//tei:profileDesc/tei:particDesc/tei:listPerson/(tei:person|tei:personGrp)[@xml:id]
return map:merge((
map:entry("id", replace($item/@xml:id/string(), $id || '-', '')),
map:entry("name", $item/(tei:name|tei:persName)/text()),
map:entry("isGroup", if($item/local-name() eq 'personGrp') then true() else false()),
if ($item/@sex) then map:entry("sex", $item/@sex/string()) else ()
))
},
"speeches": map:merge ((
for $sp in $tei//tei:body//tei:sp[@xml:id and @who]
let $key := replace($sp/@xml:id/string(), $id, $epid)
return map:entry($key, array {
tokenize($sp/@who, '\s+') ! replace(., '#' || $id || '-', '')
})
)),
"changes": array {
for $change in $tei//tei:teiHeader/tei:revisionDesc/tei:listChange/tei:change
return map {
"when": $change/@when/string(),
"text": $change/text()
}
}
}