-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcheutils.xql
161 lines (137 loc) · 6.54 KB
/
archeutils.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
xquery version "3.1";
(:~
: This module provides a couple of variables (and some helper functions) to set ease the creation of ARCHE-RDF metadata representation
: @author peter.andorfer@oeaw.ac.at
:)
module namespace archeutils="http://www.digital-archiv.at/ns/archeutils";
import module namespace app="http://www.digital-archiv.at/ns/templates" at "../modules/app.xql";
declare namespace functx = "http://www.functx.com";
declare namespace tei = "http://www.tei-c.org/ns/1.0";
declare namespace acdh="https://vocabs.acdh.oeaw.ac.at/schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace util = "http://exist-db.org/xquery/util";
declare namespace owl = "http://www.w3.org/2002/07/owl#";
declare variable $archeutils:constants_exist :=
if (doc-available($app:data||'/meta/arche_constants.rdf'))
then
true()
else
false();
declare variable $archeutils:constants :=
if ($archeutils:constants_exist)
then
doc($app:data||'/meta/arche_constants.rdf')//acdh:ACDH
else
<acdh:ACDH/>;
declare variable $archeutils:base_url := $archeutils:constants//acdh:TopColl/@url;
declare variable $archeutils:app_base_url := $archeutils:constants//acdh:AppBaseURL/text();
declare variable $archeutils:entity_url := $archeutils:base_url||"/entity";
declare variable $archeutils:available_date := <acdh:hasAvailableDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">{current-date()}</acdh:hasAvailableDate>;
declare variable $archeutils:repoobject_constants : = $archeutils:constants//acdh:RepoObject/*;
declare variable $archeutils:resource_constants : = ($archeutils:repoobject_constants, $archeutils:constants//acdh:Resource/*);
declare variable $archeutils:agents := $archeutils:constants//acdh:MetaAgents/*;
declare variable $archeutils:collstruct := $archeutils:constants//acdh:CollStruct;
declare variable $archeutils:tei_lookups := $archeutils:constants//acdh:TeiLookUps;
declare variable $archeutils:person_lookups := $archeutils:constants//acdh:PersonLookUps;
declare variable $archeutils:place_lookups := $archeutils:constants//acdh:PlaceLookUps;
declare variable $archeutils:org_lookups := $archeutils:constants//acdh:OrgLookUps;
declare variable $archeutils:default_lang := $archeutils:constants//acdh:DefaultLang/text();
(:~
: looks for a norm-data URL in any <tei:idno> and returns the first on found or the project-specific id
: @param $item The node of the entitiy e.g. tei:place, tei:person, tei:org
: @return A string used as ARCHE-ID
:)
declare function archeutils:get_entity_id($item as node()){
let $default_id := concat($archeutils:entity_url, '/', data($item/@xml:id))
let $ids := $item//tei:idno[
contains(.//text(), 'd-nb.info') or
contains(.//text(), 'geonames') or
contains(.//text(), 'viaf')
]/text()
let $result := if ($ids) then $ids else $default_id
return
$result
};
(:~
: creates RDF Metadata describing the applications basic collection structure
:
: @param $cols A sequence of names of the collection, need to match the @name attribute and are used to genereate the collections identifier
: @return An ARCHE RDF describing the collections
:)
declare function archeutils:dump_collections($cols as item()*) as node()*{
let $topcol :=
<acdh:TopCollection rdf:about="{$archeutils:base_url}">
{$archeutils:collstruct//acdh:TopColl//*}
{$archeutils:repoobject_constants}
{$archeutils:available_date}
</acdh:TopCollection>
let $childCols :=
for $x in $cols
let $col :=
<acdh:Collection rdf:about="{$archeutils:base_url||'/'||$x}">
<acdh:isPartOf rdf:resource="{$archeutils:base_url}"/>
{$archeutils:available_date}
{$archeutils:collstruct//acdh:DataColl[@name=$x]//*}
{$archeutils:repoobject_constants}
{$archeutils:available_date}
</acdh:Collection>
where $col/acdh:hasTitle
return
$col
let $RDF :=
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:acdh="https://vocabs.acdh.oeaw.ac.at/schema#"
xml:base="https://id.acdh.oeaw.ac.at/">
{$topcol}
{$childCols}
{$archeutils:agents}
</rdf:RDF>
return $RDF
};
(:~
: generates resource specific attributes derived from acdh_TeiLookUps and the passed in TEI-Document
:
: @param $doc The root element of a TEI Document (or the node from wich the provided xpaths should be evaluated)
: @return ARCHE properties
:)
declare function archeutils:populate_tei_resource($lookup as item(), $item as node()) as node()*{
for $x in $lookup/*
let $lang := if (exists(data($x/@lang))) then data($x/@lang) else $archeutils:default_lang
let $el_name := name($x)
let $el_type := data($x/@type)
let $el_xpath := $x/text()
let $el_value := try {
if ($el_type eq 'no_eval') then $el_xpath else util:eval($el_xpath)
} catch * {false()}
let $el :=
switch ($el_type)
case 'date' return element {$el_name} {attribute rdf:datatype { "http://www.w3.org/2001/XMLSchema#date" }, $el_value }
case 'resource_many' return for $res_url in $el_value return element {$el_name} {attribute rdf:resource { $res_url }}
case 'resource' return element {$el_name} {attribute rdf:resource { $el_value }}
case 'no_eval' return element {$el_name} {$el_xpath }
case 'literal_no_lang' return element {$el_name} {$el_value }
default return element {$el_name} {attribute xml:lang { $lang }, $el_value }
where $el_value[1]
return $el
};
(:~
: tries to normalize a date like string into a full date in format YYYY-MM-DD
:
: @param $date some date like string e.g. "1900" or "1900-03" or "1900-03-11"
: @param $start if true() the date will be normlized into 1900-01-01, if false into 1900-12-31
: @return the normalized year
:)
declare function archeutils:normalize-date($date as xs:string, $start as xs:boolean) as xs:string {
let $monthYear := if ($start) then ("-01", "-01-01") else ("-12", "-12-31")
let $normalized :=
if (string-length($date) eq 4)
then $date||$monthYear[2]
else if (string-length($date) eq 7)
then $date||$monthYear[1]
else if (string-length($date) eq 10)
then $date
else "1111-11-11"
return $normalized
};