forked from dariok/page2tei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage2tei-0.xsl
808 lines (759 loc) · 32.1 KB
/
page2tei-0.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns="http://www.tei-c.org/ns/1.0"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:p="http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"
xmlns:mets="http://www.loc.gov/METS/"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:local="local"
xmlns:xstring = "https://github.com/dariok/XStringUtils"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="0" />
<xd:doc>
<xd:desc>Whether to create `rs type="..."` for person/place/org (default) or `persName` etc. (false())</xd:desc>
</xd:doc>
<xsl:param name="rs" select="true()" />
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Author:</xd:b> Dario Kampkaspar, dario.kampkaspar@oeaw.ac.at | dario.kampkaspar@tu-darmstadt.de</xd:p>
<xd:p>Austrian Centre for Digital Humanities http://acdh.oeaw.ac.at | University and State Library Darmstadt
https://ulb.tu-darmstadt.de</xd:p>
<xd:p></xd:p>
<xd:p>This stylesheet, when applied to mets.xml of the PAGE output, will create (valid) TEI</xd:p>
<xd:p>While this XSLT is designed to run on many different flavours of PAGE-XMLs described by a common mets.xml
file, some special care was taken to include meta data provided by Transkribus; other meta data providers may be
included if examples are provided.</xd:p>
<xd:p></xd:p>
<xd:p><xd:b>Contributor</xd:b> Matthias Boenig, github:@tboenig</xd:p>
<xd:p>OCR-D, Berlin-Brandenburg Academy of Sciences and Humanities http://ocr-d.de/eng</xd:p>
<xd:p>extend the original XSL-Stylesheet by specific elements based on the @typing of the text region</xd:p>
<xd:p></xd:p>
<xd:p><xd:b>Contributor</xd:b> Peter Stadler, github:@peterstadler</xd:p>
<xd:p>Carl-Maria-von-Weber-Gesamtausgabe</xd:p>
<xd:p>Added corrections to tei:sic/tei:corr</xd:p>
<xd:p></xd:p>
<xd:p><xd:b>Contributor</xd:b> Till Grallert, github:@tillgrallert</xd:p>
<xd:p>Orient-Institut Beirut</xd:p>
<xd:p>Use tei:ab as fallback instead of tei:p</xd:p>
</xd:desc>
</xd:doc>
<!-- use extended string functions from https://github.com/dariok/XStringUtils -->
<xsl:include href="string-pack.xsl"/>
<xsl:param name="debug" select="false()" />
<xd:doc>
<xd:desc>helper: gather page contents</xd:desc>
</xd:doc>
<xsl:variable name="make_div">
<div>
<xsl:apply-templates select="//mets:fileSec//mets:fileGrp[@ID='PAGEXML']/mets:file" mode="text" />
</div>
</xsl:variable>
<xd:doc>
<xd:desc>Entry point: start at the top of METS.xml</xd:desc>
</xd:doc>
<xsl:template match="/mets:mets" xml:space="preserve">
<TEI>
<teiHeader>
<fileDesc>
<titleStmt>
<xsl:apply-templates select="mets:amdSec" mode="titleStmt" />
</titleStmt>
<seriesStmt>
<xsl:apply-templates select="mets:amdSec" mode="seriesStmt" />
</seriesStmt>
<sourceDesc>
<bibl>
<xsl:apply-templates select="mets:amdSec" mode="sourceDesc" />
</bibl>
</sourceDesc>
</fileDesc>
</teiHeader>
<facsimile>
<xsl:apply-templates select="mets:fileSec//mets:fileGrp[@ID='PAGEXML']/mets:file" mode="facsimile" />
</facsimile>
<text>
<body
><xsl:for-each-group select="$make_div//*[local-name() = 'div']/*" group-starting-with="*[local-name() = 'head']" >
<div xmlns="http://www.tei-c.org/ns/1.0">
<xsl:copy-of select="current-group()"/>
</div>
</xsl:for-each-group
></body>
</text>
</TEI>
</xsl:template>
<!-- create teiHeader from METS and (if present) included Transkribus Meta data -->
<xd:doc>
<xd:desc>Contents for titleStmt</xd:desc>
</xd:doc>
<xsl:template match="mets:amdSec" mode="titleStmt">
<xsl:apply-templates select="descendant::trpDocMetadata/title" />
<xsl:apply-templates select="descendant::trpDocMetadata/author" />
</xsl:template>
<xd:doc>
<xd:desc>Contents for seriesStmt</xd:desc>
</xd:doc>
<xsl:template match="mets:amdSec" mode="seriesStmt">
<xsl:apply-templates select="descendant::trpDocMetadata//colList[1]/colName" />
</xsl:template>
<xd:doc>
<xd:desc>Contents for sourceDesc</xd:desc>
</xd:doc>
<xsl:template match="mets:amdSec" mode="sourceDesc">
<xsl:apply-templates select="descendant::trpDocMetadata/title" />
<xsl:apply-templates select="descendant::trpDocMetadata/author | descendant::trpDocMetadata/writer" />
<idno type="Transkribus">
<xsl:value-of select="descendant::trpDocMetadata/docId" />
</idno>
<xsl:apply-templates select="descendant::trpDocMetadata/externalId" />
<xsl:apply-templates select="descendant::trpDocMetadata/desc" />
</xsl:template>
<xd:doc>
<xd:desc>Contents for editionStmt</xd:desc>
</xd:doc>
<xsl:template match="mets:amdSec" mode="editionStmt">
<p>TRP document creator: <xsl:value-of select="descendant::trpDocMetadata/uploader"/></p>
<xsl:apply-templates select="mets:amdSec//trpDocMetadata/desc" />
</xsl:template>
<!-- Templates for trpMetaData -->
<xd:doc>
<xd:desc>
<xd:p>The title within the Transkribus meta data</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="title">
<title>
<xsl:if test="position() = 1">
<xsl:attribute name="type">main</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</title>
</xsl:template>
<xd:doc>
<xd:desc>The author as stated in Transkribus meta data.
Will be used in the teiHeader as titleStmt/author</xd:desc>
</xd:doc>
<xsl:template match="author">
<author>
<xsl:apply-templates />
</author>
</xsl:template>
<xd:doc>
<xd:desc>The author as stated in Transkribus meta data.
Will be used in the teiHeader as titleStmt/respStmt</xd:desc>
</xd:doc>
<xsl:template match="writer">
<respStmt>
<resp>Writer</resp>
<name><xsl:apply-templates /></name>
</respStmt>
</xsl:template>
<xd:doc>
<xd:desc>The description as given in Transkribus meta data.
Will be used in sourceDesc</xd:desc>
</xd:doc>
<xsl:template match="desc">
<note><xsl:apply-templates /></note>
</xsl:template>
<xd:doc>
<xd:desc>The name of the collection from which this document was exported.
Will be used as seriesStmt/title</xd:desc>
</xd:doc>
<xsl:template match="colName">
<title><xsl:apply-templates /></title>
</xsl:template>
<xd:doc>
<xd:desc>Transkribus meta data: external ID</xd:desc>
</xd:doc>
<xsl:template match="externalId">
<idno type="external">
<xsl:value-of select="."/>
</idno>
</xsl:template>
<!-- Templates for METS -->
<xd:doc>
<xd:desc>Create tei:facsimile with @xml:id</xd:desc>
</xd:doc>
<xsl:template match="mets:file" mode="facsimile">
<xsl:variable name="file" select="document(mets:FLocat/@xlink:href, /)"/>
<xsl:variable name="numCurr" select="@SEQ"/>
<xsl:apply-templates select="$file//p:Page" mode="facsimile">
<xsl:with-param name="imageName" select="substring-after(mets:FLocat/@xlink:href, '/')" />
<xsl:with-param name="numCurr" select="$numCurr" tunnel="true" />
</xsl:apply-templates>
</xsl:template>
<xd:doc>
<xd:desc>Apply by-page</xd:desc>
</xd:doc>
<xsl:template match="mets:file" mode="text">
<xsl:variable name="file" select="document(mets:FLocat/@xlink:href, .)"/>
<xsl:variable name="numCurr" select="@SEQ"/>
<xsl:apply-templates select="$file//p:Page" mode="text">
<xsl:with-param name="numCurr" select="$numCurr" tunnel="true" />
</xsl:apply-templates>
</xsl:template>
<!-- Templates for PAGE, facsimile -->
<xd:doc>
<xd:desc>
<xd:p>Create tei:facsimile/tei:surface</xd:p>
</xd:desc>
<xd:param name="imageName">
<xd:p>the file name of the image</xd:p>
</xd:param>
<xd:param name="numCurr">
<xd:p>Numerus currens of the parent facsimile</xd:p>
</xd:param>
</xd:doc>
<xsl:template match="p:Page" mode="facsimile">
<xsl:param name="imageName" />
<xsl:param name="numCurr" tunnel="true" />
<xsl:variable name="coords" select="tokenize(p:PrintSpace/p:Coords/@points, ' ')" />
<xsl:variable name="type" select="substring-after(@imageFilename, '.')" />
<surface ulx="0" uly="0"
lrx="{@imageWidth}" lry="{@imageHeight}"
xml:id="facs_{$numCurr}">
<graphic
url="{encode-for-uri(substring-before($imageName, '.'))||'.'||$type}"
width="{@imageWidth}px"
height="{@imageHeight}px"/>
<xsl:apply-templates
select="p:PrintSpace | p:TextRegion | p:SeparatorRegion | p:GraphicRegion | p:TableRegion"
mode="facsimile" />
</surface>
</xsl:template>
<xd:doc>
<xd:desc>create the zones within facsimile/surface</xd:desc>
<xd:param name="numCurr">Numerus currens of the current page</xd:param>
</xd:doc>
<xsl:template match="p:PrintSpace | p:TextRegion | p:SeparatorRegion | p:GraphicRegion | p:TextLine" mode="facsimile">
<xsl:param name="numCurr" tunnel="true" />
<xsl:variable name="renditionValue">
<xsl:choose>
<xsl:when test="local-name(parent::*) = 'TableCell'">TableCell</xsl:when>
<xsl:when test="local-name() = 'TextRegion'">TextRegion</xsl:when>
<xsl:when test="local-name() = 'SeparatorRegion'">Separator</xsl:when>
<xsl:when test="local-name() = 'GraphicRegion'">Graphic</xsl:when>
<xsl:when test="local-name() = 'TextLine'">Line</xsl:when>
<xsl:otherwise>printspace</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="custom" as="map(xs:string, xs:string)">
<xsl:map>
<xsl:for-each-group select="tokenize(@custom||' lfd {'||$numCurr, '\} ')" group-by="substring-before(., ' ')">
<xsl:map-entry key="substring-before(., ' ')" select="string-join(substring-after(., '{'), '–')" />
</xsl:for-each-group>
</xsl:map>
</xsl:variable>
<xsl:if test="$renditionValue = ('Line', 'TableCell')">
<xsl:text>
</xsl:text>
</xsl:if>
<zone points="{p:Coords/@points}" rendition="{$renditionValue}">
<xsl:if test="$renditionValue != 'printspace'">
<xsl:attribute name="xml:id"><xsl:value-of select="'facs_'||$numCurr||'_'||@id"/></xsl:attribute>
</xsl:if>
<xsl:if test="@type">
<xsl:attribute name="subtype"><xsl:value-of select="@type"/></xsl:attribute>
</xsl:if>
<xsl:if test="map:contains($custom, 'structure') and not(@type)">
<xsl:attribute name="subtype" select="substring-after(substring-before(map:get($custom, 'structure'), ';'), ':')" />
</xsl:if>
<xsl:apply-templates select="p:TextLine" mode="facsimile" />
<xsl:if test="not($renditionValue= ('Line', 'Graphic', 'Separator', 'printspace', 'TableCell'))">
<xsl:text>
</xsl:text>
</xsl:if>
</zone>
</xsl:template>
<xd:doc>
<xd:desc>Create the zone for a table</xd:desc>
<xd:param name="numCurr">Numerus currens of the current page</xd:param>
</xd:doc>
<xsl:template match="p:TableRegion" mode="facsimile">
<xsl:param name="numCurr" tunnel="true" />
<zone points="{p:Coords/@points}" rendition="Table">
<xsl:attribute name="xml:id"><xsl:value-of select="'facs_'||$numCurr||'_'||@id"/></xsl:attribute>
<xsl:apply-templates select="p:TableCell//p:TextLine" mode="facsimile" />
</zone>
</xsl:template>
<xd:doc>
<xd:desc>create the page content</xd:desc>
<xd:param name="numCurr">Numerus currens of the current page</xd:param>
</xd:doc>
<!-- Templates for PAGE, text -->
<xsl:template match="p:Page" mode="text">
<xsl:param name="numCurr" tunnel="true" />
<pb facs="#facs_{$numCurr}" n="{$numCurr}" xml:id="img_{format-number($numCurr, '0000')}"/>
<xsl:apply-templates select="p:TextRegion | p:SeparatorRegion | p:GraphicRegion | p:TableRegion" mode="text">
<xsl:with-param
name="center"
tunnel="true"
select="number(@imageWidth) div 2"
as="xs:double" />
</xsl:apply-templates>
</xsl:template>
<xd:doc>
<xd:desc>
<xd:p>create specific elements based on the @typing of the text region</xd:p>
<xd:p>PAGE labels for text region see: https://www.primaresearch.org/tools/PAGELibraries
caption observed
header observed
footer observed
page-number observed
drop-capital ignored
credit ignored
floating ignored
signature-mark observed
catch-word observed
marginalia observed
footnote observed
footnote-continued observed
endnote ignored
TOC-entry ignored
list-label ignored
other observed
</xd:p>
</xd:desc>
<xd:param name="numCurr"/>
<xd:param name="center" />
</xd:doc>
<xsl:template match="p:TextRegion" mode="text">
<xsl:param name="numCurr" tunnel="true" />
<xsl:param name="center" tunnel="true" as="xs:double" />
<xsl:variable name="custom" as="map(*)">
<xsl:apply-templates select="@custom" />
</xsl:variable>
<xsl:choose>
<xsl:when test="@type = 'heading'" xml:space="preserve">
<head facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine"
/></head></xsl:when>
<xsl:when test="@type = 'caption'">
<figure>
<head facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine" /></head>
</figure>
</xsl:when>
<xsl:when test="@type = 'header'" xml:space="preserve">
<fw type="header" place="top" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine"
/></fw></xsl:when>
<xsl:when test="@type = 'footer'" xml:space="preserve">
<fw type="header" place="bottom" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine"
/></fw></xsl:when>
<xsl:when test="@type = 'catch-word'" xml:space="preserve">
<fw type="catch" place="bottom" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine"
/></fw></xsl:when>
<xsl:when test="@type = 'signature-mark'" xml:space="preserve">
<fw place="bottom" type="sig" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine"
/></fw></xsl:when>
<xsl:when test="@type = 'marginalia'">
<xsl:variable name="side">
<xsl:choose>
<xsl:when test="number(substring-before(p:Coords/@points, ',')) gt $center">margin-right</xsl:when>
<xsl:otherwise>margin-left</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<note place="{$side}" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine" /></note>
</xsl:when>
<xsl:when test="@type = 'footnote'">
<note place="foot" n="[footnote reference]" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine" /></note>
</xsl:when>
<xsl:when test="@type = 'footnote-continued'">
<note place="foot" n="[footnote-continued reference]" facs="#facs_{$numCurr}_{@id}"><xsl:apply-templates select="p:TextLine" /></note>
</xsl:when>
<xsl:when test="@type = ('other', 'paragraph')">
<xsl:text>
</xsl:text>
<p facs="#facs_{$numCurr}_{@id}">
<xsl:apply-templates select="p:TextLine" />
</p>
</xsl:when>
<!-- the fallback option should be a semantically open element such as <ab> -->
<xsl:otherwise xml:space="preserve">
<ab facs="#facs_{$numCurr}_{@id}" type="{@type}{$custom?structure?type}"
><xsl:apply-templates select="p:TextLine" />
</ab></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xd:doc>
<xd:desc>create a table</xd:desc>
<xd:param name="numCurr"/>
</xd:doc>
<xsl:template match="p:TableRegion" mode="text">
<xsl:param name="numCurr" tunnel="true" />
<xsl:text>
</xsl:text>
<table facs="#facs_{$numCurr}_{@id}">
<xsl:for-each-group select="p:TableCell" group-by="@row">
<xsl:sort select="@col" />
<xsl:text>
</xsl:text>
<row n="{@row}">
<xsl:apply-templates select="current-group()" />
</row>
</xsl:for-each-group>
</table>
</xsl:template>
<xd:doc>
<xd:desc>create table cells</xd:desc>
<xd:param name="numCurr"/>
</xd:doc>
<xsl:template match="p:TableCell">
<xsl:param name="numCurr" tunnel="true" />
<xsl:text>
</xsl:text>
<cell facs="#facs_{$numCurr}_{@id}" n="{@col}">
<xsl:apply-templates select="@rowSpan | @colSpan" />
<xsl:attribute name="rend">
<xsl:value-of select="number((xs:boolean(@leftBorderVisible), false())[1])" />
<xsl:value-of select="number((xs:boolean(@topBorderVisible), false())[1])" />
<xsl:value-of select="number((xs:boolean(@rightBorderVisible), false())[1])" />
<xsl:value-of select="number((xs:boolean(@bottomBorderVisible), false())[1])" />
</xsl:attribute>
<xsl:apply-templates select="p:TextLine" />
</cell>
</xsl:template>
<xd:doc>
<xd:desc>rowspan -> rows</xd:desc>
</xd:doc>
<xsl:template match="@rowSpan">
<xsl:choose>
<xsl:when test=". > 1">
<xsl:attribute name="rows" select="." />
</xsl:when>
</xsl:choose>
</xsl:template>
<xd:doc>
<xd:desc>colspan -> cols</xd:desc>
</xd:doc>
<xsl:template match="@colSpan">
<xsl:choose>
<xsl:when test=". > 1">
<xsl:attribute name="cols" select="." />
</xsl:when>
</xsl:choose>
</xsl:template>
<xd:doc>
<xd:desc>
Combine taggings marked with “continued” – cf. https://github.com/dariok/page2tei/issues/10
Thanks to @thodel for reporting.
</xd:desc>
<xd:param name="context" />
</xd:doc>
<xsl:template name="continuation">
<xsl:param name="context" />
<xsl:for-each select="$context/node()">
<xsl:choose>
<xsl:when test="@continued
and following-sibling::*[1][self::tei:lb]
and string-length(normalize-space(following-sibling::node()[1])) = 0">
<xsl:element name="{local-name()}">
<xsl:sequence select="@*[not(local-name() = 'continued')]" />
<xsl:sequence select="node()" />
<lb>
<xsl:sequence select="following-sibling::*[1]/@*" />
<xsl:attribute name="break" select="'no'" />
</lb>
<xsl:sequence select="following-sibling::*[2]/node()" />
</xsl:element>
</xsl:when>
<xsl:when test="(self::tei:lb
and preceding-sibling::*[1]/@continued
and string-length(normalize-space(preceding-sibling::node()[1])) = 0
and following-sibling::node()[1]/@continued)
or (@continued and preceding-sibling::node()[1][self::tei:lb])
or (self::text()
and normalize-space() = ''
and preceding-sibling::node()[1]/@continued
and following-sibling::node()[1][self::tei:lb])" />
<xsl:otherwise>
<xsl:sequence select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xd:doc>
<xd:desc>Converts one line of PAGE to one line of TEI</xd:desc>
<xd:param name="numCurr">Numerus currens, to be tunneled through from the page level</xd:param>
</xd:doc>
<xsl:template match="p:TextLine">
<xsl:param name="numCurr" tunnel="true" />
<xsl:variable name="text" select="p:TextEquiv/p:Unicode"/>
<xsl:variable name="custom" as="text()*">
<xsl:for-each select="tokenize(@custom, '\}')">
<xsl:choose>
<xsl:when test="string-length() < 1 or starts-with(., 'readingOrder') or starts-with(normalize-space(), 'structure')" />
<xsl:otherwise>
<xsl:value-of select="normalize-space()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="starts" as="map(*)">
<xsl:map>
<xsl:if test="count($custom) > 0">
<xsl:for-each-group select="$custom" group-by="substring-before(substring-after(., 'offset:'), ';')">
<xsl:map-entry key="xs:int(current-grouping-key())" select="current-group()" />
</xsl:for-each-group>
</xsl:if>
</xsl:map>
</xsl:variable>
<xsl:variable name="ends" as="map(*)">
<xsl:map>
<xsl:if test="count($custom) > 0">
<xsl:for-each-group select="$custom" group-by="xs:int(substring-before(substring-after(., 'offset:'), ';'))
+ xs:int(substring-before(substring-after(., 'length:'), ';'))">
<xsl:map-entry key="current-grouping-key()" select="current-group()" />
</xsl:for-each-group>
</xsl:if>
</xsl:map>
</xsl:variable>
<xsl:variable name="prepped">
<xsl:for-each select="0 to string-length($text)">
<xsl:if test=". > 0"><xsl:value-of select="substring($text, ., 1)"/></xsl:if>
<xsl:for-each select="map:get($starts, .)">
<!--<xsl:sort select="substring-before(substring-after(.,'offset:'), ';')" order="ascending"/>-->
<!-- end of current tag -->
<xsl:sort select="xs:int(substring-before(substring-after(., 'offset:'), ';'))
+ xs:int(substring-before(substring-after(., 'length:'), ';'))" order="descending" />
<xsl:sort select="substring(., 1, 3)" order="ascending" />
<xsl:element name="local:m">
<xsl:attribute name="type" select="normalize-space(substring-before(., ' '))" />
<xsl:attribute name="o" select="substring-after(., 'offset:')" />
<xsl:attribute name="pos">s</xsl:attribute>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="map:get($ends, .)">
<xsl:sort select="substring-before(substring-after(.,'offset:'), ';')" order="descending"/>
<xsl:sort select="substring(., 1, 3)" order="descending"/>
<xsl:element name="local:m">
<xsl:attribute name="type" select="normalize-space(substring-before(., ' '))" />
<xsl:attribute name="o" select="substring-after(., 'offset:')" />
<xsl:attribute name="pos">e</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="prepared">
<xsl:for-each select="$prepped/node()">
<xsl:choose>
<xsl:when test="@pos = 'e'">
<xsl:variable name="position" select="count(preceding-sibling::node())" />
<xsl:variable name="o" select="@o" />
<xsl:variable name="id" select="@type" />
<xsl:variable name="precs"
select="preceding-sibling::local:m[@pos = 's' and preceding-sibling::local:m[@o = $o]]" />
<xsl:for-each select="$precs">
<xsl:variable name="so" select="@o"/>
<xsl:variable name="myP" select="count(following-sibling::local:m[@pos='e' and @o=$so]/preceding-sibling::node())"/>
<xsl:if test="following-sibling::local:m[@pos = 'e' and @o=$so
and $myP > $position] and not(@type = $id)">
<local:m type="{@type}" pos="e" o="{@o}" prev="{$myP||'.'||$position||($myP > $position)}" />
</xsl:if>
</xsl:for-each>
<xsl:sequence select="." />
<xsl:for-each select="$precs">
<xsl:variable name="so" select="@o"/>
<xsl:variable name="myP" select="count(following-sibling::local:m[@pos='e' and @o=$so]/preceding-sibling::node())"/>
<xsl:if test="following-sibling::local:m[@pos = 'e' and @o=$so
and $myP > $position] and not(@type = $id)">
<local:m type="{@type}" pos="s" o="{@o}" prev="{$myP||'.'||$position||($myP > $position)}" />
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<!-- TODO parameter to create <l>...</l> - #1 -->
<xsl:text>
</xsl:text>
<lb facs="#facs_{$numCurr}_{@id}">
<xsl:if test="@custom">
<xsl:variable name="pos" select="xs:integer(substring-before(substring-after(@custom, 'index:'), ';')) + 1" />
<xsl:attribute name="n">
<xsl:text>N</xsl:text>
<xsl:value-of select="format-number($pos, '000')" />
</xsl:attribute>
</xsl:if>
</lb>
<xsl:apply-templates select="$prepared/text()[not(preceding-sibling::local:m)]" />
<xsl:apply-templates select="$prepared/local:m[@pos='s']
[count(preceding-sibling::local:m[@pos='s']) = count(preceding-sibling::local:m[@pos='e'])]" />
<!--[not(preceding-sibling::local:m[1][@pos='s'])]" />-->
</xsl:template>
<xd:doc>
<xd:desc>Starting milestones for (possibly nested) elements</xd:desc>
</xd:doc>
<xsl:template match="local:m[@pos='s']">
<xsl:variable name="o" select="@o"/>
<xsl:variable name="custom" as="map(*)">
<xsl:map>
<xsl:variable name="t" select="tokenize(@o, ';')"/>
<xsl:if test="count($t) > 1">
<xsl:for-each select="$t[. != '']">
<xsl:map-entry key="normalize-space(substring-before(., ':'))" select="normalize-space(substring-after(., ':'))" />
</xsl:for-each>
</xsl:if>
</xsl:map>
</xsl:variable>
<xsl:variable name="elem">
<local:t>
<xsl:sequence select="following-sibling::node()
intersect following-sibling::local:m[@o=$o]/preceding-sibling::node()" />
</local:t>
</xsl:variable>
<xsl:choose>
<xsl:when test="@type = 'textStyle'">
<hi rend="{xstring:substring-before-if-ends(substring-after(substring-after(@o, 'length'), ';'), '}')}">
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</hi>
</xsl:when>
<xsl:when test="@type = 'supplied'">
<supplied reason="">
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</supplied>
</xsl:when>
<xsl:when test="@type = 'abbrev'">
<choice>
<expan><xsl:value-of select="replace(map:get($custom, 'expansion'), '\\u0020', ' ')"/></expan>
<abbr>
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</abbr>
</choice>
</xsl:when>
<xsl:when test="@type = 'sic'">
<choice>
<corr><xsl:value-of select="replace(map:get($custom, 'correction'), '\\u0020', ' ')"/></corr>
<sic>
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</sic>
</choice>
</xsl:when>
<xsl:when test="@type = 'date'">
<date>
<!--<xsl:variable name="year" select="if(map:keys($custom) = 'year') then format-number(xs:integer(map:get($custom, 'year')), '0000') else '00'"/>
<xsl:variable name="month" select=" if(map:keys($custom) = 'month') then format-number(xs:integer(map:get($custom, 'month')), '00') else '00'"/>
<xsl:variable name="day" select=" if(map:keys($custom) = 'day') then format-number(xs:integer(map:get($custom, 'day')), '00') else '00'"/>
<xsl:variable name="when" select="$year||'-'||$month||'-'||$day" />
<xsl:if test="$when != '0000-00-00'">
<xsl:attribute name="when" select="$when" />
</xsl:if>-->
<xsl:for-each select="map:keys($custom)">
<xsl:if test=". != 'length' and . != ''">
<xsl:attribute name="{.}" select="map:get($custom, .)" />
</xsl:if>
</xsl:for-each>
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</date>
</xsl:when>
<xsl:when test="@type = 'person'">
<xsl:variable name="elName" select="if ($rs) then 'rs' else 'persName'" />
<xsl:element name="{$elName}">
<xsl:if test="$rs">
<xsl:attribute name="type">person</xsl:attribute>
</xsl:if>
<xsl:if test="$custom('lastname') != '' or $custom('firstname') != ''">
<xsl:attribute name="key" select="replace($custom('lastname'), '\\u0020', ' ') || ', ' || replace($custom('firstname'), '\\u0020', ' ')" />
</xsl:if>
<xsl:if test="$custom('continued')">
<xsl:attribute name="continued" select="true()" />
</xsl:if>
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</xsl:element>
</xsl:when>
<xsl:when test="@type = 'place'">
<xsl:variable name="elName" select="if ($rs) then 'rs' else 'placeName'" />
<xsl:element name="{$elName}">
<xsl:if test="$rs">
<xsl:attribute name="type">place</xsl:attribute>
</xsl:if>
<xsl:if test="$custom('placeName') != ''">
<xsl:attribute name="key" select="replace($custom('placeName'), '\\u0020', ' ')" />
</xsl:if>
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</xsl:element>
</xsl:when>
<xsl:when test="@type = 'organization'">
<xsl:variable name="elName" select="if ($rs) then 'rs' else 'orgName'" />
<xsl:element name="{$elName}">
<xsl:if test="$rs">
<xsl:attribute name="type">org</xsl:attribute>
</xsl:if>
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{@type}">
<xsl:call-template name="elem">
<xsl:with-param name="elem" select="$elem" />
</xsl:call-template>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="following-sibling::local:m[@pos='e' and @o=$o]/following-sibling::node()[1][self::text()]" />
</xsl:template>
<xd:doc>
<xd:desc>Process what's between a pair of local:m</xd:desc>
<xd:param name="elem"/>
</xd:doc>
<xsl:template name="elem">
<xsl:param name="elem" />
<xsl:choose>
<xsl:when test="$elem//local:m">
<xsl:apply-templates select="$elem/local:t/text()[not(preceding-sibling::local:m)]" />
<xsl:apply-templates select="$elem/local:t/local:m[@pos='s']
[not(preceding-sibling::local:m[1][@pos='s'])]" />
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$elem/local:t/node()" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xd:doc>
<xd:desc>Leave out possibly unwanted parts</xd:desc>
</xd:doc>
<xsl:template match="p:Metadata" mode="text" />
<xd:doc>
<xd:desc>Parse the content of an attribute such as @custom into a map.</xd:desc>
</xd:doc>
<xsl:template match="@custom" as="map(*)">
<xsl:map>
<xsl:for-each select="tokenize(., '\}')[normalize-space() != '']">
<xsl:map-entry key="substring-before(normalize-space(), ' ')">
<xsl:map>
<xsl:for-each select="tokenize(substring-after(., '{'), ';')[normalize-space() != '']">
<xsl:map-entry key="substring-before(., ':')" select="substring-after(., ':')" />
</xsl:for-each>
</xsl:map>
</xsl:map-entry>
</xsl:for-each>
</xsl:map>
</xsl:template>
<xd:doc>
<xd:desc>Text nodes to be copied</xd:desc>
</xd:doc>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>