transform2teisimple.xqm 16.2 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
18
19
20
 : 
 : 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";

declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace test="http://exist-db.org/xquery/xqsuite";

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

(:~ 
 : The main function initiates the transformation of a given notebook.
 : 
 : TODO: adapt to several input files?
 : 
 : :)
30
declare function fontane-simple:main($file as xs:string) as xs:string? {
MRodz's avatar
MRodz committed
31
32
33
34
35
36
37
  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
38
39
40
41
42
    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"))]
    
43
    let $tei := <TEI xmlns="http://www.tei-c.org/ns/1.0">
MRodz's avatar
MRodz committed
44
        {$doc//tei:teiHeader}
MRodz's avatar
MRodz committed
45
46
47
48
49
        <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
50
      </TEI>
51
    return xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple.xml", $tei)
MRodz's avatar
MRodz committed
52
53
};

MRodz's avatar
MRodz committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(:~ 
 : 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
68
69
70
71
72
73
    for $node in $nodes
      return
        typeswitch ($node)
        case text()
          return
            $node
74
            
MRodz's avatar
MRodz committed
75
76
        case element(tei:graphic)
            return
77
                element tei:graphic {
MRodz's avatar
MRodz committed
78
79
                    $node/@*
                }
80
                
MRodz's avatar
MRodz committed
81
82
83
84
85
86
        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
87
                ()
88
                
MRodz's avatar
MRodz committed
89
90
91
92
93
94
        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())
95
                
MRodz's avatar
MRodz committed
96
97
98
99
100
101
102
103
        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())
104
105
                
        case element(tei:add) return
106
107
108
109
110
111
            if($node/@type = "edited_text" 
            or $node/child::tei:seg[@type = "multiphrase"]) then
                element tei:add {
                    $node/@*,
                    fontane-simple:transform($node/node())
                }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
            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
                $node
            else
                ()       
                
MRodz's avatar
MRodz committed
128
129
130
        case element(tei:fw)
            return
                ()
131
                
MRodz's avatar
MRodz committed
132
133
        case element(tei:line)
            return
134
135
136
137
138
139
                if(count($node/*) = 1 and $node/child::tei:fw) then
                    ()
                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 {
MRodz's avatar
MRodz committed
140
                        fontane-simple:transform($node/node())
141
142
143
144
145
146
147
                    }
                else if($node/@type = "item" 
                and fontane-simple:is-transposed($node)) then
                    ()
                else
                    fontane-simple:transform($node/node())
                        
MRodz's avatar
MRodz committed
148
149
150
151
152
153
154
        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
155
                        element tei:handShift {
MRodz's avatar
MRodz committed
156
157
158
159
160
161
162
163
164
165
                            $node/@*
                        }
                    else
                        ()
                
(:                if($node/following-sibling::*[1][count(./*) = 1 :)
(:                    and child::tei:fw or child::tei:stamp]):)
(:                    then:)
(:                        ():)
(:                    else:)
166
(:                        element tei:handShift {:)
MRodz's avatar
MRodz committed
167
168
169
170
171
(:                            $node/@*:)
(:                        }:)
        case element(tei:stamp)
            return
                ()
172
                
MRodz's avatar
MRodz committed
173
174
        case element(tei:seg)
            return
175
                if(count($node/*) = 1 and 
MRodz's avatar
MRodz committed
176
                ($node/child::tei:stamp or $node/child::tei:metamark[@function = "caret"]))
MRodz's avatar
MRodz committed
177
178
                    then
                        ()
MRodz's avatar
MRodz committed
179
180
                else if(matches($node/@style, "underline")) then
                    fontane-simple:transform($node/node())
181
182
                else if($node/@type = "initials" or $node/@type = "monogram" 
                or $node/@style or $node/@type = "multiphrase")
MRodz's avatar
MRodz committed
183
                    then
184
                        element tei:seg {
MRodz's avatar
MRodz committed
185
186
187
                            $node/@*,
                            fontane-simple:transform($node/node())
                        }
MRodz's avatar
MRodz committed
188
                else if($node/@type = "auction_number" 
MRodz's avatar
MRodz committed
189
190
                or $node/@type = "cancel"
                or $node/@type = "abort")
MRodz's avatar
MRodz committed
191
192
                    then
                        ()
193
194
                else
                    fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
195
196
197
198
199
                    
        case element(tei:hi) return
            element tei:hi {
                fontane-simple:transform($node/node())
            }
200
201
202
        
        case element(tei:anchor) return
            $node
203
                        
MRodz's avatar
MRodz committed
204
        case element(tei:surface)
MRodz's avatar
MRodz committed
205
            return 
MRodz's avatar
MRodz committed
206
207
208
209
                if(matches($node/@n, "cover"))
                    then
                        (if(not($node/@n = "outer_front_cover"))
                            then
210
                                element tei:pb {
MRodz's avatar
MRodz committed
211
212
213
                                    if($node/@n = "inner_front_cover" 
                                    or $node/@n = "outer_back_cover")
                                        then
214
                                            attribute type {"flyleaf"} 
MRodz's avatar
MRodz committed
215
                                        else
216
                                            attribute type {"cover"}
MRodz's avatar
MRodz committed
217
218
219
                                }
                            else
                                (),
220
221
222
223
                        element tei:div {
                            attribute facs {$node/@facs},
                            attribute xml:id {$node/@xml:id},
                            attribute type {$node/@n},
MRodz's avatar
MRodz committed
224
225
226
227
                            fontane-simple:transform($node/node())
                        })
                else if(matches($node/@n, "[0-9]{1,4}[rv]{1}"))
                    then
228
229
                        (element tei:pb{
                            attribute n {$node/@n},
MRodz's avatar
MRodz committed
230
231
                            if($node/@facs)
                                then
232
                                    attribute facs {$node/@facs}
MRodz's avatar
MRodz committed
233
234
235
236
237
238
                                else
                                    ()
                        }, 
                        (: if all descendant text nodes shoudln't be serialized, z.g. parent::fw or parent::stamp :)
                        if($node)
                            then
239
240
                                element tei:div{
                                    attribute type {"page"},
MRodz's avatar
MRodz committed
241
242
243
244
245
                                    fontane-simple:transform($node/node())
                                }
                            else
                                ())
                else if($node/@type = "label" and
MRodz's avatar
MRodz committed
246
247
248
249
                    (contains($node/@subtype, "Fontane")
                    or contains($node/@subtype, "Hersteller"))    
                )
                    then
250
251
                        element tei:div {
                            attribute type {"label"},
MRodz's avatar
MRodz committed
252
253
                            fontane-simple:transform($node/node())
                        }
MRodz's avatar
MRodz committed
254
255
256
257
                else if($node/@type = "fragment" 
                    and $node/descendant::text()[not(ancestor::tei:fw or ancestor::tei:stamp)])
                    then
                        fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
258
259
                    else
                        ()
260
                        
261
262
263
264
265
        case element(tei:gap)
            return
                element tei:gap {
                    $node/@*
                }
266
                
267
268
269
270
271
272
        case element(tei:metamark) return
            if($node/@function = "integrate" 
            or $node/@function = "authorial_note") then
                element tei:ab {
                    $node/@*
                }
273
274
275
276
277
            else if($node/@function = "paragraph") then
                element tei:ab {
                    attribute type {"paragraph-line-special"},
                    fontane-simple:transform($node/node())
                }
278
279
            else
                ()
280
281
282
283
284
        
        (: TODO: check if correct :)
        case element(tei:surplus) return
            ()
            
MRodz's avatar
MRodz committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
        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
306
307
308
309
310
311
312
313
            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())
                    }            
314
315
316
317
            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
318
319
            else
                fontane-simple:transform($node/node())
320
                
MRodz's avatar
MRodz committed
321
        case element(tei:figure) return
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
            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"}
                    }
                    else
                        ()
                else
                    $node
363
                    
MRodz's avatar
MRodz committed
364
365
        case element(tei:figDesc) return
            $node
366
            
MRodz's avatar
MRodz committed
367
368
        case element(tei:ref) return
            $node
369
370
371
372
373
374
            
        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
375
        default return
376
            fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
377
378
379
380
381
382
383
384
385
386
387
388
389
};

(: 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()
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
};

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
406
};