transform2teisimple.xqm 22.5 KB
Newer Older
MRodz's avatar
MRodz committed
1
2
3
4
5
xquery version "3.1";

(:~ 
 : This modules handles the conversion of the Fontante-TEI/XML into TEISimple
 : for the edited text. The resulting TEISimple is the basis for the "Editerter
MRodz's avatar
MRodz committed
6
 : Text" view on the website and the book and represent the latest layer of text.
MRodz's avatar
MRodz committed
7
8
9
10
11
12
13
14
15
16
17
 : 
 : The resulting TEISimple orientates itself toward the so-called "SUB-Schema"
 : which is a subset for TEISimple that is recommended for future projects that
 : are held by the Göttingen State and University Library (SUB).
 : 
 : @author Michelle Rodzis
 : @version 0.1
 :)

module namespace fontane-simple="http://fontane-nb.dariah.eu/teisimple";

MRodz's avatar
MRodz committed
18

MRodz's avatar
MRodz committed
19
20
21
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace test="http://exist-db.org/xquery/xqsuite";

22
import module namespace config="http://textgrid.de/ns/SADE/config" at "../config/config.xqm";
MRodz's avatar
MRodz committed
23
import module namespace console="http://exist-db.org/xquery/console";
MRodz's avatar
MRodz committed
24
import module namespace functx = "http://www.functx.com";
MRodz's avatar
MRodz committed
25
26
27
28
29
30
31

(:~ 
 : The main function initiates the transformation of a given notebook.
 : 
 : TODO: adapt to several input files?
 : 
 : :)
32
declare function fontane-simple:main($file as xs:string) as xs:string? {
MRodz's avatar
MRodz committed
33
34
35
36
37
38
39
  let $doc := 
    try {
      (doc("/db/sade-projects/textgrid/data/xml/data/" || $file))
    } catch * {
      (console:log("It was not possible to open the requested file " || $file))
    }
    
MRodz's avatar
MRodz committed
40
41
42
43
44
    let $front-covers := $doc//tei:sourceDoc/tei:surface[contains(@n, "front_cover")]/*
    let $back-covers := $doc//tei:sourceDoc/tei:surface[contains(@n, "back_cover")]/*
    let $content := $doc//tei:sourceDoc/tei:surface[not(contains(@n, "cover") 
        and matches(@n, "spine"))]
    
45
    let $tei := <TEI xmlns="http://www.tei-c.org/ns/1.0">
MRodz's avatar
MRodz committed
46
        {$doc//tei:teiHeader}
MRodz's avatar
MRodz committed
47
48
49
50
51
        <text>
            <front>{fontane-simple:transform($front-covers)}</front>
            <body>{fontane-simple:transform($content)}</body>
            <back>{fontane-simple:transform($back-covers)}</back>
        </text>
MRodz's avatar
MRodz committed
52
      </TEI>
53
    return xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple.xml", $tei)
MRodz's avatar
MRodz committed
54
55
};

MRodz's avatar
MRodz committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(:~ 
 : Recursivly iterates the passed nodes and converts them according to the 
 : requirements for the "Edierter Text". 
 : 
 : The requirements are part of the encoding documentation, c.f.
 : https://fontane-nb.dariah.eu/doku.html.
 : 
 : 
 : @author Michelle Rodzis
 : @param $nodes
 : @return $node()* a TEISimple element
 : 
 :)
declare function fontane-simple:transform($nodes as node()*) as node()* {
MRodz's avatar
MRodz committed
70
71
72
    for $node in $nodes
      return
        typeswitch ($node)
MRodz's avatar
MRodz committed
73
74
75
76
77
78
79
        case text() return
            local:prepare-text($node)
        
        case element(tei:lb) return
            if($node[@break = "keepHyphen"]) then
                ()
            else
80
                local:keep-element($node)
MRodz's avatar
MRodz committed
81
82
83
84
85
                
        case element(tei:g) return
            if($node[@ref = "#vds"]) then
                ()
            else if($node/@ref ="#rth" or $node/@ref ="#hb") then
86
                local:keep-element($node)
MRodz's avatar
MRodz committed
87
88
            else
                fontane-simple:transform($node/node())
89
                
MRodz's avatar
MRodz committed
90
91
92
93
94
95
        case element(tei:del) return
            if($node/parent::tei:restore) then
                fontane-simple:transform($node/node())
            else if($node/descendant::tei:restore) then
                fontane-simple:transform($node/descendant::tei:restore)
            else
MRodz's avatar
MRodz committed
96
                ()
97
                
MRodz's avatar
MRodz committed
98
99
100
101
102
103
        case element(tei:restore) return
            if(count($node/child::*) = 1 and $node/child::tei:del
            and $node/ancestor::tei:del) then
                ()
            else
                fontane-simple:transform($node/node())
104
                
MRodz's avatar
MRodz committed
105
106
107
108
109
110
111
112
        case element(tei:retrace) return
            if($node/@rend) then
                element tei:seg {
                    $node/@rend,
                    fontane-simple:transform($node/node())
                }
            else
                fontane-simple:transform($node/node())
113
114
                
        case element(tei:add) return
115
116
            if($node/@type = "edited_text" 
            or $node/child::tei:seg[@type = "multiphrase"]) then
117
                local:keep-element($node)
MRodz's avatar
MRodz committed
118
119
            else if($node/@cause ="catchword" or $node/@cause ="unclear") then
                ()
MRodz's avatar
MRodz committed
120
121
            else if($node/@rend ="|") then
                fontane-simple:transform($node/node())
122
123
124
125
126
127
128
129
130
131
132
133
            else if(not($node/@xml:id)) then
                fontane-simple:transform($node/node())
            else if(fontane-simple:is-transposed($node)) then
                let $corresp := //tei:metamark[matches(@target, $node/@xml:id)]
                return
                    (fontane-simple:transform($corresp/node()),
                    fontane-simple:transform($node/node()))
            else
                fontane-simple:transform($node/node())
                
        case element(tei:addSpan) return
            if($node/@type = "edited_text") then
134
                local:keep-element($node)
135
136
137
            else
                ()       
                
138
139
        case element(tei:fw) return
            ()
140
                
141
142
143
        case element(tei:line) return
            if(count($node/*) = 1 and $node/child::tei:fw) then
                ()
MRodz's avatar
MRodz committed
144
145
            else if($node/@type = "heading") then
                local:make-heading($node)
146
147
148
149
            else if(not($node/@type = "item")) then
                fontane-simple:transform($node/node())
            else if($node/@type = "item" and not($node/@xml:id)) then
                element tei:item {
150
                    fontane-simple:transform($node/node())
151
152
153
                }
            else if($node/@type = "item" 
            and fontane-simple:is-transposed($node)) then
MRodz's avatar
MRodz committed
154
                ()            
155
156
            else
                fontane-simple:transform($node/node())
157
                        
158
159
160
161
162
163
164
165
166
        case element(tei:handShift) return
            (: tei:handShift without @new denote the duct of the current 
            writer or a change of writing medium :)
            if($node/@new and (fontane-simple:is-hand-contemporary($node/@new) or
            $node/@new = "#Friedrich_Fontane") or not($node/@new))
                then
                    local:keep-element($node)
                else
                    ()
MRodz's avatar
MRodz committed
167
168
169
170
171
172
                
(:                if($node/following-sibling::*[1][count(./*) = 1 :)
(:                    and child::tei:fw or child::tei:stamp]):)
(:                    then:)
(:                        ():)
(:                    else:)
173
(:                        element tei:handShift {:)
MRodz's avatar
MRodz committed
174
175
(:                            $node/@*:)
(:                        }:)
176
177
        case element(tei:stamp) return
            ()
178
                
179
180
181
182
183
        case element(tei:seg) return
            if(count($node/*) = 1 and 
            ($node/child::tei:stamp or $node/child::tei:metamark[@function = "caret"]))
                then
                    ()
MRodz's avatar
MRodz committed
184
185
            else if($node/@type = "heading") then
                local:make-heading($node)
186
187
188
            else if(matches($node/@style, "underline") 
            and not(matches($node/@style, "vertical-align"))) then
                fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
189
190
191
192
193
194
            else if($node/@style) then
                element{QName("http://www.tei-c.org/ns/1.0", $node/name())}{
                    attribute rendition {$node/@style},
                    $node/(@* except @style),
                    fontane-simple:transform($node/node())
                }                   
195
            else if($node/@type = "initials" or $node/@type = "monogram" 
MRodz's avatar
MRodz committed
196
            or $node/@type = "multiphrase")
197
198
199
200
                then
                    local:keep-element($node)
            else if($node/@type = "auction_number" 
            or $node/@type = "cancel"
MRodz's avatar
MRodz committed
201
202
            or $node/@type = "abort"
            or $node/@function ="unknown")
203
204
205
206
                then
                    ()
            else
                fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
207
208
                    
        case element(tei:hi) return
209
            local:keep-element($node)
210
211
        
        case element(tei:anchor) return
212
            local:keep-element($node)
213
                        
214
        case element(tei:surface) return 
MRodz's avatar
MRodz committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
            if(matches($node/@n, "cover")) then
                (if(not($node/@n = "outer_front_cover"))
                    then
                        element tei:pb {
                            if($node/@n = "inner_front_cover" 
                            or $node/@n = "outer_back_cover")
                                then
                                    attribute type {"flyleaf"} 
                                else
                                    attribute type {"cover"}
                        }
                    else
                        (),
                element tei:div {
                    attribute type {$node/@n},
                    $node/((@* except @facs) except @n),
                    fontane-simple:transform($node/node())
                })
            else if(matches($node/@n, "[0-9IVXMCD]{1,7}[rv]{1}")
            and $node/@type = "clipping") then
                (local:make-pb($node),
MRodz's avatar
MRodz committed
236
237
                (if(not($node/@subtype = "Kalenderblatt"
                or $node/@subtype = "Zeitungsausschnitt_Fragment")) then
MRodz's avatar
MRodz committed
238
                    element tei:div{
MRodz's avatar
MRodz committed
239
                        $node/((@* except @facs) except @n),
240
                        fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
                    }
                else
                    ()))
            else if(matches($node/@n, "[0-9IVXMCD]{1,7}[rv]{1}")
            and $node/@type = "fragment") then
                (local:make-pb($node), 
                element tei:div{
                    $node/((@* except @facs) except @n),
                    fontane-simple:transform($node/node())
                })            
            else if(matches($node/@n, "[0-9IVXMCD]{1,7}[rv]{1}")
            and $node/@type = "pocket") then
                local:make-pb($node)            
            else if(matches($node/@n, "[0-9IVXMCD]{1,7}[rv]{1}")
            and $node/@type = "label" and $node/@subtype = "Oblate") then
                ()
            else if(matches($node/@n, "[0-9IVXMCD]{1,7}[rv]{1}")) then
                (local:make-pb($node), 
                element tei:div{
                    attribute type {"page"},
                    $node/((@* except @facs) except @n),
                    fontane-simple:transform($node/node())
                })
264
265
266
            else if($node/@type = "label" and
                (contains($node/@subtype, "Fontane")
                or contains($node/@subtype, "Hersteller"))    
MRodz's avatar
MRodz committed
267
268
269
270
271
                ) then
                element tei:div {
                    attribute type {"label"},
                    fontane-simple:transform($node/node())
                }
MRodz's avatar
MRodz committed
272
273
            else
                ()
MRodz's avatar
MRodz committed
274
275
276
277
278
279
        
        case element(tei:milestone) return
            if($node/@unit = "illustration") then
                ()
            else
                fontane-simple:transform($node/node())
280
281
282
                    
        case element(tei:gap) return
            local:keep-element($node)
283
                
284
285
286
287
288
289
        case element(tei:metamark) return
            if($node/@function = "integrate" 
            or $node/@function = "authorial_note") then
                element tei:ab {
                    $node/@*
                }
290
291
292
293
294
295
            else if($node/@function = "placeholder" 
            or $node/@function ="etc."
            or $node/@function ="caret"
            or $node/@function ="footnotes"
            or $node/@function ="ellipsis"
            or $node/@function = "paragraph") then
MRodz's avatar
MRodz committed
296
297
298
                element tei:ab {
                    attribute type {$node/@function},
                    fontane-simple:transform($node/node())
299
                }            
300
301
            else
                ()
302
303
304
305
306
        
        (: TODO: check if correct :)
        case element(tei:surplus) return
            ()
            
MRodz's avatar
MRodz committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
        case element(tei:zone) return
            if(matches($node/@style, "border-style:solid") 
            and not(matches($node/@style, "border-radius"))
            and not($node/@rend = "border-style:house")) then
                element tei:div {
                    attribute type {"frame"},
                    fontane-simple:transform($node/node())
                }
            else if(matches($node/@rend, "border-bottom-style:brace")) then
                (fontane-simple:transform($node/node()),
                element tei:ab {
                    attribute type {"bottom-brace"}
                })                    
            else if($node/@type = "cancel") then
                ()
            else if($node/@type = "marked_off") then
                element tei:seg {
                    $node/@type,
                    $node/@xml:id,
                    fontane-simple:transform($node/node())
                }
MRodz's avatar
MRodz committed
328
329
330
331
332
333
334
            else if($node/@type = "highlighted") then
                if($node/child::tei:zone[@type = "highlighted"]) then
                    fontane-simple:transform($node/node())
                else
                    element tei:hi {
                        attribute type {"vertical-mark"},
                        fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
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
                    }
            else if($node/@type = "illustration") then
                if(not($node//tei:figure/parent::tei:del)) then
                    element {QName("http://www.tei-c.org/ns/1.0", "ab")}{
                        (if($node/child::tei:zone[@type = "illustration"]) then
                            attribute type {"composed-sketch"}
                        else
                            (attribute type {"sketch"},
                            if($node/parent::tei:zone[@type = "illustration"]) then
                                attribute rendition {"margin-left:" || $node/@ulx 
                                || "cm; " || "margin-top:" || $node/@uly || "cm"}
                            else
                                ()
                            )
                        ),
                        fontane-simple:transform($node/node())
                    }
                else 
                    ()
            else if($node/parent::tei:zone/@type = "illustration") then
                element {QName("http://www.tei-c.org/ns/1.0", "seg")}{
                    attribute type {"caption"},
                    attribute rendition {"margin-left:" || $node/@ulx || "cm; " 
                        || "margin-top:" || $node/@uly || "cm"},
                    fontane-simple:transform($node/node())
                }
MRodz's avatar
MRodz committed
361
362
            else if($node/@type = "heading") then
                local:make-heading($node)                 
MRodz's avatar
MRodz committed
363
364
365
366
367
368
369
370
371
372
            else if($node/@type = "list" or $node/@type = "item") then
                element {QName("http://www.tei-c.org/ns/1.0", $node/@type)}{
                    $node/(@* except @type),
                    fontane-simple:transform($node/node())
                }            
            else if($node/@type = "legend") then
                element {QName("http://www.tei-c.org/ns/1.0", "div")}{
                    $node/@*,
                    fontane-simple:transform($node/node())
                }            
373
374
375
376
            else if(not($node/@xml:id)) then
                fontane-simple:transform($node/node())
            else if($node/@xml:id and fontane-simple:is-transposed($node)) then
                ()
MRodz's avatar
MRodz committed
377
378
            else
                fontane-simple:transform($node/node())
379
                
MRodz's avatar
MRodz committed
380
        case element(tei:figure) return
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
            if(count($node/child::*) = 1 and $node/child::tei:figDesc) then
                (: genealogy lines probably shouldn't be displayed, but I still have to 
                check that. in case they should be serialized, I leave the code :)
(:                if(matches($node/descendant::tei:ref, "Stammbaumverbindungslinie")) then:)
(:                    element tei:seg {:)
(:                        $node/@*,:)
(:                        fontane-simple:transform($node/node()):)
(:                    }:)
(:                else if(matches($node/descendant::tei:ref, "Schlusslinie")):)
                if(matches($node/descendant::tei:ref, "Schlusslinie"))
                    then
                        element tei:ab {
                            switch ($node/descendant::tei:ref)
                                case "horizontale einfache Schlusslinie" return 
                                    attribute type {"long-end-line"}                                    
                                case "Schlusslinie; horizontale Halbschleife von links oben nach rechts" return 
                                    attribute type {"long-end-line"}
                                case "horizontale einfache Schlusslinie (gewellt)" return 
                                    attribute type {"long-end-line-wavy"}
                                case "Schlusslinien; horizontale Schleife von links oben nach rechts unten" return 
                                    attribute type {"bottom-brace-short"}
                                default return 
                                    attribute type {"end-line"}  
                        }
                else if(matches($node/descendant::tei:ref, "Absatzlinie")
                (: in case of double paragraph lines the single lines are 
                encoded with "oberer" resp. "unterer Teil", but we only 
                serialize the encoding for the upper line :)
                and not(matches($node/descendant::tei:ref, "unterer Teil"))
                and not(matches($node/descendant::tei:figDesc, "unsicher"))) then
                    element tei:ab {
                        if(matches($node/descendant::tei:ref, "doppelt"))
                            then
                                attribute type {"short-paragraph-line-double"}
                            else
                                attribute type {"short-paragraph-line"}
                    }
MRodz's avatar
MRodz committed
418
419
420
421
                else if($node/parent::tei:zone[@type = "illustration"]) then
                    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
                        $node/@*
                    }
422
                else
MRodz's avatar
MRodz committed
423
424
425
                    ()
            else
                local:keep-element($node)
426
                    
MRodz's avatar
MRodz committed
427
        case element(tei:note) return
428
429
            if($node/@type = "authorial" 
            and not($node/@subtype = "footnote")) then
MRodz's avatar
MRodz committed
430
431
                ()
            else 
432
                local:keep-element($node)
MRodz's avatar
MRodz committed
433
434
435
436
437
438
439
440
441
                
        case element(tei:certainty) return
            element {QName("http://www.tei-c.org/ns/1.0", "note")}{
                attribute type {"editorial"},
                attribute subtype {"certainty"},
                $node/@cert,
                $node/@target,
                fontane-simple:transform($node/node())
            }
MRodz's avatar
MRodz committed
442
                    
MRodz's avatar
MRodz committed
443
        case element(tei:figDesc) return
444
            local:keep-element($node)
445
            
MRodz's avatar
MRodz committed
446
        case element(tei:ref) return
447
            local:keep-element($node)
MRodz's avatar
MRodz committed
448
449
        
        case element(tei:space) return
450
            local:keep-element($node)
MRodz's avatar
MRodz committed
451
452
453
454
455
456
457
458
            
        case element(tei:choice) return
            local:keep-element($node)
            
        case element(tei:abbr) return
            local:keep-element($node)
            
        case element(tei:expan) return
MRodz's avatar
MRodz committed
459
460
461
462
463
464
            local:keep-element($node)        
            
        case element(tei:rs) return
            local:keep-element($node)        
            
        case element(tei:date) return
465
466
467
468
469
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
                attribute when {$node/@when-iso},
                $node/(@* except @when-iso),
                fontane-simple:transform($node/node())
            }
470
471
472
473
474
475
            
        case element(tei:ptr) return
            let $target-id := substring-after($node/@target, "#")
            let $target := $node/ancestor::tei:TEI//*[matches(@xml:id, $target-id)]
            let $stripped-target := fontane-simple:strip-element($target)
            return fontane-simple:transform($stripped-target)
MRodz's avatar
MRodz committed
476
            
MRodz's avatar
MRodz committed
477
        default return
478
            fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
479
480
481
482
483
484
485
486
487
488
489
490
491
};

(: TODO: adapt to general XML :)
declare function fontane-simple:is-hand-contemporary($hand as xs:string) as xs:boolean {
    let $hand := replace($hand, "#", "")
    let $file := doc("/db/sade-projects/textgrid/data/xml/data/16b00.xml")
    let $handNote := $file//tei:handNote[@xml:id = $hand]
    return 
        if($handNote/@script = "contemporary")
            then
                true()
            else
                false()
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
};

declare function fontane-simple:is-transposed($node as node()) as xs:boolean {
    let $root := $node/ancestor::tei:TEI
    return
        if($root//tei:ptr[contains(@target, $node/@xml:id)]) then
            true()
        else
            false()
};

declare function fontane-simple:strip-element($node as node()) {
    element {$node/name()} {
        $node/@type,
        fontane-simple:transform($node/node())
    }
MRodz's avatar
MRodz committed
508
509
};

MRodz's avatar
MRodz committed
510
declare function local:prepare-text($node as text()) as text()? {
MRodz's avatar
MRodz committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    let $cleared-hyphen := 
        if(ends-with($node, "-") and not(local:keep-hyphen($node))) then
            text {functx:substring-before-last($node, "-")}
        else if(ends-with($node, "⸗") and not(local:keep-hyphen($node))) then
            text {functx:substring-before-last($node, "⸗")}
        else
            $node
    let $cleared-round-s := replace($cleared-hyphen, "ſ", "s")
    return text {$cleared-round-s}
};

declare function local:keep-hyphen($node as text()) as xs:boolean {
    if($node/parent::tei:line/following-sibling::*[1][self::tei:lb[@break = "keepHyphen"]])
        then
            true()
    else
        false()
};

declare function local:keep-element($node as node()) as node() {
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
        $node/@*,
        fontane-simple:transform($node/node())
    }
MRodz's avatar
MRodz committed
535
536
};

MRodz's avatar
MRodz committed
537
538
539
declare function local:has-valid-text($node as node()) as xs:boolean {
    (: TODO :)
    true()
MRodz's avatar
MRodz committed
540
541
542
543
544
545
};

declare function local:make-pb($node as node()) as element(tei:pb) {
    element {QName("http://www.tei-c.org/ns/1.0", "pb")}{
        $node/@n
    }
MRodz's avatar
MRodz committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
};

declare function local:make-heading($node as node()) as element(tei:head) {
    element tei:head {
        (if($node/descendant::tei:seg[matches(@style, "font-size")]) then
            attribute type {local:get-fontsize($node)}
        else 
            attribute type {"default"}
        ),
        $node/@subtype,
        fontane-simple:transform($node/node())
    }
};

declare function local:get-fontsize($node as node()) as xs:string {
    (: example for style: "font-size:large; letter-spacing:0.2cm; text-decoration:underline" :)
    let $tmp := substring-after($node/descendant-or-self::tei:seg[matches(@style, "font-size")]/@style, "font-size:")
    return substring-before($tmp, ";")
MRodz's avatar
MRodz committed
564
};