-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviaf.xsl
More file actions
306 lines (266 loc) · 13.3 KB
/
viaf.xsl
File metadata and controls
306 lines (266 loc) · 13.3 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?xml version="1.0" encoding="UTF-8"?>
<!-- get RDF/XML from viaf.org and transform it to TEI
USAGE:
target/bin/xslt.sh -config:saxon-local.xml -xsl:xsl/viaf/viaf.xsl -it id=VAIF_ID
USAGE: get RDF/XML
target/bin/xslt.sh -config:saxon-local.xml -xsl:xsl/viaf/viaf.xsl -it output-format=rdf id=VAIF_ID
USAGE: transform a RDF/XML source file to TEI
target/bin/xslt.sh -xsl:xsl/viaf/viaf.xsl -s:VAIF-RDF.XML
http:send-request#3 must be registered as extension function.
<resources>
<extensionFunction>org.expath.httpclient.saxon.SendRequestFunction</extensionFunction>
</resources>
See https://github.com/expath/expath-http-client-java/tree/main
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:http="http://expath.org/ns/http-client"
xmlns="http://www.tei-c.org/ns/1.0" xmlns:bgn="http://bibliograph.net/"
xmlns:dbo="http://dbpedia.org/ontology/" xmlns:genont="http://www.w3.org/2006/gen/ont#"
xmlns:pto="http://www.productontology.org/id/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:re="http://oclcsrw.google.code/redirect" xmlns:schema="http://schema.org/"
xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:umbel="http://umbel.org/umbel#"
xmlns:wdt="http://www.wikidata.org/prop/direct/" xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:skos="http://www.w3.org/2004/02/skos/core#" exclude-result-prefixes="#all" version="3.0"
default-mode="tei">
<xsl:output method="xml" indent="true"/>
<xsl:param name="id" as="xs:string" select="'90045354'" required="false"/>
<xsl:param name="entry-id-prefix" as="xs:string" select="'viaf_'" required="false"/>
<xsl:param name="viaf-base-url" as="xs:string" select="'https://viaf.org/viaf/'"/>
<xsl:param name="viaf-base-uri" as="xs:string" select="'http://viaf.org/viaf/'"/>
<xsl:param name="output-format" as="xs:string" select="'tei'" static="true"/>
<xsl:param name="config-url" as="xs:string?" select="()"/>
<xsl:param name="config" as="document-node(element(config))"
select="($config-url ! doc(.), $default-config)[1]"/>
<xsl:variable name="default-config" as="document-node(element(config))">
<xsl:document>
<config xmlns="">
<files>
<file match="(^http://data.bnf.fr/ark:)(.*)" base="$1" id="$2"/>
<file match="(^http://www.idref.fr/)(([^/]*)(/id)$)" base="$1" id="$2"/>
<file match="(^http://id.loc.gov/authorities/names/)(.*)" base="$1" id="$2"/>
<file match="(^http://www.wikidata.org/entity/)(.*)" base="$1" id="$2"/>
</files>
<prefLabel>
<scheme>http://viaf.org/authorityScheme/LC</scheme>
</prefLabel>
<prefLabel>
<scheme>http://viaf.org/authorityScheme/BNF</scheme>
</prefLabel>
</config>
</xsl:document>
</xsl:variable>
<xsl:param name="viaf-source-id-base" as="xs:string" select="'http://viaf.org/viaf/sourceID/'"/>
<xsl:variable name="viaf-resource" as="xs:string" select="$viaf-base-uri || $id"/>
<xsl:variable name="viaf-url" as="xs:string" select="$viaf-base-url || $id"/>
<xsl:param name="request" as="element(http:request)">
<http:request method="GET">
<http:header name="accept" value="application/rdf+xml"/>
</http:request>
</xsl:param>
<xsl:template name="xsl:initial-template">
<xsl:call-template name="from-viaf"/>
</xsl:template>
<!-- entry point -->
<xsl:template name="from-viaf" use-when="function-available('http:send-request', 3)">
<xsl:variable name="graph" as="node()*" select="http:send-request($request, $viaf-url, ())"/>
<xsl:apply-templates select="$graph" _mode="{$output-format}">
<xsl:with-param name="type" as="xs:anyURI*"
select="$graph/rdf:RDF/rdf:Description[@rdf:about eq $viaf-resource] ! rdf:type(.)"
tunnel="true"/>
</xsl:apply-templates>
</xsl:template>
<!-- alternative entry when http:send-request is not available -->
<xsl:template name="from-viaf" use-when="not(function-available('http:send-request', 3))">
<xsl:message terminate="yes">
<xsl:text xml:space="preserve">function Q{http://expath.org/ns/http-client}send-request#3 not available</xsl:text>
</xsl:message>
</xsl:template>
<!-- returns all rdf:type s assigned to a resource -->
<xsl:function name="rdf:type" as="xs:anyURI*" visibility="public">
<xsl:param name="resource" as="element(rdf:Description)"/>
<xsl:sequence select="$resource/rdf:type/@rdf:resource ! xs:anyURI(.)"/>
</xsl:function>
<!-- the rdf mode outputs the RDF/XML data from VIAF as is -->
<xsl:template match="/" mode="rdf">
<xsl:copy-of select="."/>
</xsl:template>
<!-- the tei mode outputs TEI from from VIAF RDF/XML -->
<xsl:mode name="tei" on-no-match="shallow-skip"/>
<xsl:template mode="tei"
match="rdf:Description[rdf:type(.) = xs:anyURI('http://schema.org/Person')]">
<person>
<xsl:call-template name="identifier"/>
<xsl:call-template name="names"/>
<xsl:call-template name="dates"/>
<xsl:call-template name="idno"/>
</person>
</xsl:template>
<xsl:template mode="tei"
match="rdf:Description[rdf:type(.) = xs:anyURI('http://schema.org/Place')]">
<place>
<xsl:call-template name="identifier"/>
<xsl:call-template name="names"/>
<xsl:call-template name="idno"/>
</place>
</xsl:template>
<xsl:template name="identifier">
<xsl:context-item as="element(rdf:Description)"/>
<xsl:attribute name="xml:id" select="$entry-id-prefix || $id"/>
</xsl:template>
<xsl:template name="idno">
<xsl:context-item as="element(rdf:Description)"/>
<xsl:variable name="context" as="element(rdf:Description)" select="."/>
<xsl:if test="starts-with($context/@rdf:about, $viaf-base-uri)">
<idno xml:base="http://viaf.org/viaf/">
<xsl:value-of select="$context/dcterms:identifier"/>
</idno>
</xsl:if>
<xsl:message use-when="system-property('debug') eq 'true'">
<xsl:text>count of nafs </xsl:text>
<xsl:value-of select="$config/* => count()"/>
</xsl:message>
<xsl:for-each select="$config/*:config/*:files/*:file">
<xsl:variable name="file" as="element()" select="."/>
<xsl:variable name="naf" as="attribute()?"
select="($context/schema:sameAs/@rdf:resource | $context/schema:sameAs/rdf:Description/@rdf:about)[matches(., $file/@match)][1]"/>
<xsl:message use-when="system-property('debug') eq 'true'">
<xsl:text>idno of </xsl:text>
<xsl:sequence select="$file"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="$naf"/>
</xsl:message>
<xsl:if test="$naf">
<idno>
<xsl:attribute name="xml:base" select="replace($naf, @match, @base)"/>
<xsl:value-of select="replace($naf, @match, @id)"/>
</idno>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="dates">
<xsl:context-item as="element(rdf:Description)" use="required"/>
<xsl:if test="schema:birthDate">
<birth calendar="gregorian">
<xsl:attribute name="when">
<xsl:value-of select="schema:birthDate"/>
</xsl:attribute>
</birth>
</xsl:if>
<xsl:if test="schema:deathDate">
<death calendar="gregorian">
<xsl:attribute name="when">
<xsl:value-of select="schema:deathDate"/>
</xsl:attribute>
</death>
</xsl:if>
</xsl:template>
<xsl:template name="names">
<xsl:context-item as="element(rdf:Description)" use="required"/>
<xsl:variable name="context" as="element(rdf:Description)" select="."/>
<xsl:variable name="about" as="xs:string" select="@rdf:about"/>
<!-- graph: all resources focusing the current resource -->
<xsl:variable name="graph" as="element(rdf:Description)*"
select="., parent::rdf:RDF/rdf:Description[foaf:focus/@rdf:resource = $about]"/>
<xsl:for-each select="$config/*:config/*:prefLabel">
<xsl:variable name="preferredLabel" as="element()" select="."/>
<xsl:variable as="element()*" name="labels">
<xsl:for-each select="*:scheme">
<xsl:variable name="scheme" as="xs:string" select="."/>
<xsl:variable name="naf-data" as="element(rdf:Description)?"
select="$graph[skos:inScheme/@rdf:resource = $scheme][1]"/>
<xsl:choose>
<xsl:when test="$naf-data">
<xsl:element
name="{rdf:type($context) => rdf:type-to-tei-name-element()}">
<xsl:copy-of select="$preferredLabel/@*"/>
<xsl:attribute name="sameAs" select="$naf-data/@rdf:about"/>
<xsl:value-of select="$naf-data/skos:prefLabel"/>
</xsl:element>
<xsl:message>
<xsl:text>no name found in </xsl:text>
<xsl:value-of select="$scheme"/>
</xsl:message>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$labels[1]"/>
</xsl:for-each>
</xsl:template>
<xsl:function name="rdf:type-to-tei-name-element" as="xs:string" visibility="public">
<xsl:param name="type" as="xs:anyURI*"/>
<xsl:choose>
<xsl:when test="$type = xs:anyURI('http://schema.org/Person')">
<xsl:value-of select="'persName'"/>
</xsl:when>
<xsl:when test="$type = xs:anyURI('http://schema.org/Place')">
<xsl:value-of select="'placeName'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'unknown'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!-- the mode config outputs a config file from RDF/XML -->
<xsl:mode name="config" on-no-match="shallow-skip"/>
<xsl:template mode="config"
match="rdf:Description[@rdf:about = $viaf-resource and rdf:type(.) => rdf:type-to-tei-name-element() != 'unknown']">
<xsl:variable name="about" as="xs:string" select="@rdf:about"/>
<config xmlns="">
<files>
<xsl:for-each select="schema:sameAs">
<xsl:variable name="resource" as="xs:string"
select="(@rdf:resource | rdf:Description/@rdf:about)"/>
<xsl:choose>
<xsl:when test="$resource">
<file>
<xsl:attribute name="match" select="$resource ! rdf:iri-format(.)"/>
<xsl:attribute name="base">
<xsl:text>$1</xsl:text>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:text>$2</xsl:text>
</xsl:attribute>
</file>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>could not parse sameAs: </xsl:text>
<xsl:sequence select="."/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</files>
<xsl:for-each
select="parent::rdf:RDF/rdf:Description[foaf:focus/@rdf:resource = $about and exists(skos:inScheme)]">
<prefLabel xmlns="">
<match>
<xsl:value-of
select="@rdf:about => replace(concat($viaf-source-id-base, '([^%]*)%.*'), '$1')"
/>
</match>
<scheme>
<xsl:value-of select="skos:inScheme/@rdf:resource"/>
</scheme>
</prefLabel>
</xsl:for-each>
</config>
</xsl:template>
<!-- returns a regex for extracting the core ID from an resource IRI -->
<xsl:function name="rdf:iri-format" as="xs:string">
<xsl:param as="xs:string" name="iri"/>
<xsl:choose>
<xsl:when test="matches($iri, 'ark:')">
<xsl:value-of select="replace($iri, '^(.*ark:)(.*)', '(^$1)(.*)')"/>
</xsl:when>
<xsl:when test="matches($iri, '/id$')">
<xsl:value-of select="replace($iri, '^(.*/)[^/]*/id$', '(^$1)(([^/]*)(/id)\$)')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="replace($iri, '^(.*/)([^/])*$', '(^$1)(.*)')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>