forked from dracor-org/engdracor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep2dracor.xsl
474 lines (441 loc) · 15.2 KB
/
ep2dracor.xsl
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:ep="http://earlyprint.org/ns/1.0"
xmlns:d="http://dracor.org/ns/1.0"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="ep d tei fn map"
version="3.0">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="authors" select="document('authors.xml')"/>
<xsl:param name="index" select="document('index.xml')"/>
<xsl:param name="spelling">w</xsl:param>
<xsl:param name="outputdirectory">tei</xsl:param>
<xsl:param name="speakersjson"/>
<xsl:variable
name="tcpid"
select="replace(tokenize(base-uri(.), '/')[last()], '.xml', '')"/>
<xsl:variable name="meta" select="$index//item[@sourceid eq $tcpid]"/>
<xsl:variable name="tcpid-base">
<xsl:value-of select="tokenize($tcpid, '_')[1]"/>
</xsl:variable>
<!--
In some documents (e.g. A16527_01.xml) only the main part of the TCP ID
before the number suffix (_01) is used to prefix the xml:ids while in others
(e.g. A04632_09.xml) the entire ID is used.
Therefore, to remove the EarlyPrint ID prefix we first try to remove the
full ID including a possible suffix and then the main part.
-->
<xsl:function name="d:de-prefix">
<xsl:param name="id"/>
<xsl:sequence
select="replace(replace($id, $tcpid || '-', ''), $tcpid-base || '-', '')"
/>
</xsl:function>
<xsl:function name="d:re-prefix">
<xsl:param name="id"/>
<xsl:sequence select="$meta/@id || '-' || d:fix-id(d:de-prefix($id))" />
</xsl:function>
<xsl:function name="d:fix-id">
<xsl:param name="id"/>
<xsl:sequence select="replace($id, '[^-.\w]', '_')" />
</xsl:function>
<xsl:function name="d:id-to-name">
<xsl:param name="id"/>
<xsl:sequence select="
fn:string-join(
tokenize(d:de-prefix($id), '[-_]')
! concat(upper-case(substring(., 1, 1)), substring(., 2)), ' ')"
/>
</xsl:function>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$meta">
<xsl:variable
name="target"
select="concat($outputdirectory, '/', $meta/@slug, '.xml')"/>
<xsl:message select="$tcpid || ': ' || $target"/>
<xsl:result-document href="{$target}">
<xsl:apply-templates select="tei:TEI"/>
</xsl:result-document>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Unknown document ID: </xsl:text>
<xsl:value-of select="$tcpid"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:function name="ep:get-content">
<xsl:param name="w"/>
<xsl:choose>
<xsl:when test="$spelling = 'orig'">
<xsl:choose>
<xsl:when test="$w/@orig">
<xsl:value-of select="$w/@orig"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$w"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$spelling = 'w'">
<xsl:value-of select="$w"/>
</xsl:when>
<xsl:when test="$spelling = 'reg'">
<xsl:choose>
<xsl:when test="$w/@reg">
<xsl:value-of select="$w/@reg"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$w"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$spelling = 'lemma'">
<xsl:choose>
<xsl:when test="$w/@lemma">
<xsl:value-of select="$w/@lemma"/>
</xsl:when>
<xsl:when test="$w/@reg">
<xsl:value-of select="$w/@reg"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$w"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:function>
<xsl:template match="tei:w">
<xsl:variable name="content">
<xsl:value-of select="ep:get-content(.)"/>
</xsl:variable>
<xsl:value-of select="$content"/>
<xsl:if
test="
(not(@join) or (@join ne 'right')) and
(not(following::*[1][name() = 'pc' and not(@join)])) and
(not(following::*[1][@join = 'left']))">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="tei:pc">
<xsl:value-of select="text()"/>
<xsl:if
test="
(not(@join) or (@join ne 'right')) and
(not(following::*[1][name() = 'pc' and not(@join)])) and
(not(following::*[1][@join = 'left']))">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template
match="tei:*[tei:w or tei:pc]"
>
<xsl:copy>
<xsl:apply-templates select="@*|*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tei:TEI">
<TEI xml:id="{$meta/@id}" xml:lang="eng">
<xsl:apply-templates/>
</TEI>
</xsl:template>
<xsl:template match="tei:teiHeader">
<teiHeader>
<fileDesc>
<titleStmt>
<xsl:call-template name="titles"/>
<xsl:call-template name="authors"/>
</titleStmt>
<xsl:call-template name="publicationStmt"/>
<xsl:call-template name="sourceDesc"/>
</fileDesc>
<xsl:call-template name="profileDesc"/>
<xsl:call-template name="revisionDesc"/>
</teiHeader>
<standOff>
<xsl:if test="//tei:xenoData/ep:epHeader/(ep:creationYear|ep:publicationYear)[string-length() > 0]">
<!--
FIXME: is ep:creationYear reliably the year of writing and can we use
//tei:biblFull[@n='printed source']/tei:publicationStmt/tei:date[@type='publication_date']
as print year?
-->
<listEvent>
<xsl:if test="//tei:xenoData/ep:epHeader/ep:creationYear[string-length() > 0]">
<event type="written">
<xsl:attribute name="when" select="//tei:xenoData/ep:epHeader/ep:creationYear"/>
<desc/>
</event>
</xsl:if>
<xsl:if test="//tei:xenoData/ep:epHeader/ep:publicationYear[string-length() > 0]">
<event type="print">
<xsl:attribute name="when" select="//tei:xenoData/ep:epHeader/ep:publicationYear"/>
<desc/>
</event>
</xsl:if>
</listEvent>
</xsl:if>
<xsl:if test="$meta/@wikidata">
<listRelation>
<relation name="wikidata">
<xsl:attribute name="active">
<xsl:text>https://dracor.org/entity/</xsl:text>
<xsl:value-of select="$meta/@id"/>
</xsl:attribute>
<xsl:attribute name="passive">
<xsl:text>http://www.wikidata.org/entity/</xsl:text>
<xsl:value-of select="$meta/@wikidata"/>
</xsl:attribute>
</relation>
</listRelation>
</xsl:if>
</standOff>
</xsl:template>
<!-- strip facsimile information -->
<xsl:template match="tei:facsimile|@facs" />
<!-- strip @xml:id except for div, sp and stage -->
<xsl:template match="@xml:id"/>
<xsl:template match="(tei:div|tei:sp|tei:stage|tei:pb)/@xml:id">
<xsl:attribute name="xml:id" select="d:re-prefix(.)"/>
</xsl:template>
<xsl:template match="tei:sp[@xml:id]">
<xsl:copy>
<xsl:choose>
<xsl:when test="$speakersjson">
<xsl:variable name="id" select="@xml:id/string()"/>
<xsl:variable name="speeches" as="map(*)" select="json-doc($speakersjson)?speeches"/>
<xsl:variable name="who" select="map:get($speeches, $id)"/>
<xsl:if test="count($who?*) > 0">
<xsl:attribute name="who" select="$who?* ! concat('#', $meta/@id, '-', .)"/>
</xsl:if>
<xsl:apply-templates select="@*[local-name() != 'who']" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@*" />
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<!-- fix @who references -->
<xsl:template match="tei:sp/@who">
<xsl:attribute
name="who"
select="tokenize(., ' ') ! concat('#', d:re-prefix(.))"
/>
</xsl:template>
<!-- strip machine generated castlist -->
<xsl:template match="tei:div[@type='machine-generated_castlist']" />
<!-- strip textual notes -->
<xsl:template match="tei:div[@type='textual_notes']" />
<xsl:template name="titles">
<xsl:variable name="ep-title" select="//tei:xenoData/ep:epHeader/ep:title[1]"/>
<xsl:choose>
<xsl:when test="$meta/@title">
<title type="main">
<xsl:value-of select="$meta/@title"/>
</title>
</xsl:when>
<xsl:when test="$ep-title">
<title type="main">
<xsl:value-of select="normalize-space($ep-title)"/>
</title>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="//tei:fileDesc/tei:titleStmt/tei:title"/>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$meta/@subtitle">
<title type="sub">
<xsl:value-of select="$meta/@subtitle"/>
</title>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="authors">
<xsl:variable
name="author-elems"
select="$authors//author/play[. eq $meta/@id]/parent::*"/>
<xsl:choose>
<xsl:when test="$author-elems">
<xsl:for-each select="$author-elems">
<xsl:sort select="play[. = $meta/@id]/@position"/>
<xsl:copy select="./tei:author">
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="//tei:xenoData/ep:epHeader/ep:author">
<!-- match author name with authors.xml -->
<xsl:variable name="name" select="ep:name"/>
<xsl:variable
name="match"
select="$authors//author/match[. eq $name]/parent::*"/>
<xsl:choose>
<xsl:when test="$match">
<xsl:copy select="$match/tei:author">
<xsl:apply-templates/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<author>
<xsl:comment select="normalize-space(.)"/>
<xsl:variable
name="parts" select="tokenize(normalize-space(.), ', *')"/>
<persName>
<forename>
<xsl:value-of select="$parts[2]"/>
</forename>
<surname>
<xsl:value-of select="$parts[1]"/>
</surname>
</persName>
</author>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="publicationStmt">
<publicationStmt>
<publisher xml:id="dracor">DraCor</publisher>
<idno type="URL">https://dracor.org/</idno>
<availability>
<licence>
<ab>CC0 1.0</ab>
<ref target="https://creativecommons.org/publicdomain/zero/1.0/">Licence</ref>
</licence>
</availability>
<idno type="wikidata" xml:base="http://www.wikidata.org/entity/"></idno>
</publicationStmt>
</xsl:template>
<xsl:template name="sourceDesc">
<sourceDesc>
<bibl type="digitalSource">
<name>EarlyPrint Project</name>
<idno type="URL">
<xsl:text>https://texts.earlyprint.org/works/</xsl:text>
<xsl:value-of select="$tcpid"/>
<xsl:text>.xml</xsl:text>
</idno>
<!-- FIXME: add source URL (Bitbucket or website?) -->
<availability>
<!-- FIXME? -->
<xsl:apply-templates select="//tei:teiHeader/tei:fileDesc/tei:publicationStmt/tei:availability/*"/>
</availability>
</bibl>
<!-- FIXME: originalSource? -->
<xsl:apply-templates select="//tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:biblFull"/>
</sourceDesc>
</xsl:template>
<xsl:template name="profileDesc">
<profileDesc>
<xsl:call-template name="particDesc"/>
<xsl:call-template name="textClass"/>
</profileDesc>
</xsl:template>
<xsl:template name="textClass">
<xsl:variable name="subgenre" select="//tei:xenoData/ep:epHeader/ep:subgenre"/>
<xsl:if test="$subgenre = ('comedy', 'tragedy', 'tragicomedy')">
<textClass>
<classCode scheme="http://www.wikidata.org/entity/">
<xsl:choose>
<xsl:when test="$subgenre = ('comedy')">
<xsl:text>Q40831</xsl:text>
</xsl:when>
<xsl:when test="$subgenre = ('tragedy')">
<xsl:text>Q80930</xsl:text>
</xsl:when>
<xsl:when test="$subgenre = ('tragicomedy')">
<xsl:text>Q192881</xsl:text>
</xsl:when>
</xsl:choose>
</classCode>
</textClass>
</xsl:if>
</xsl:template>
<xsl:template name="particDesc">
<xsl:choose>
<xsl:when test="$speakersjson">
<xsl:call-template name="particDesc-from-json"/>
</xsl:when>
<xsl:when test="//tei:sp/@who">
<xsl:call-template name="particDesc-from-who"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="particDesc-from-who">
<particDesc>
<listPerson>
<xsl:for-each select="distinct-values(//tei:sp/@who/tokenize(., ' '))">
<person xml:id="{d:re-prefix(.)}">
<persName>
<xsl:value-of select="d:id-to-name(.)"/>
</persName>
</person>
</xsl:for-each>
</listPerson>
</particDesc>
</xsl:template>
<xsl:template name="particDesc-from-json">
<xsl:variable name="speakers" select="fn:json-doc($speakersjson)"/>
<particDesc>
<listPerson>
<xsl:for-each select="$speakers?particDesc?*">
<xsl:choose>
<xsl:when test="?isGroup">
<personGrp xml:id="{concat($meta/@id, '-', ?id)}">
<xsl:if test="?sex">
<xsl:attribute name="sex" select="?sex"/>
</xsl:if>
<name>
<xsl:value-of select="?name"/>
</name>
</personGrp>
</xsl:when>
<xsl:otherwise>
<person xml:id="{concat($meta/@id, '-', ?id)}">
<xsl:if test="?sex">
<xsl:attribute name="sex" select="?sex"/>
</xsl:if>
<persName>
<xsl:value-of select="?name"/>
</persName>
</person>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</listPerson>
</particDesc>
</xsl:template>
<xsl:template name="revisionDesc">
<xsl:if test="$speakersjson">
<xsl:variable name="speakers" select="fn:json-doc($speakersjson)"/>
<xsl:if test="count($speakers?changes?*) > 0">
<revisionDesc>
<listChange>
<xsl:for-each select="$speakers?changes?*">
<change when="{?when}">
<xsl:value-of select="?text"/>
</change>
</xsl:for-each>
</listChange>
</revisionDesc>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>