tidysimple.xqm 16.6 KB
Newer Older
1
2
3
4
5
6
7
8
xquery version "3.1";

(:~
 : This modules handles the conversion of the Fontante-TEI/XML into TEI simplePrint
 : for the edited text. The resulting TEI simplePrint is the basis for the "Editerter
 : Text" (edited text) view on the website and the book. It represents the latest
 : layer of text.
 :
MRodz's avatar
MRodz committed
9
 : @author Michelle Weidling
10
11
12
13
14
15
 : @version 0.1
 : @since TODO
 :)

module namespace tidySimple ="http://fontane-nb.dariah.eu/tidysimple";

MRodz's avatar
MRodz committed
16

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

MRodz's avatar
MRodz committed
20
import module namespace config="http://textgrid.de/ns/SADE/config" at "../../config/config.xqm";
21
import module namespace console="http://exist-db.org/xquery/console";
22
import module namespace functx="http://www.functx.com";
MRodz's avatar
MRodz committed
23
import module namespace presort="http://fontane-nb.dariah.eu/presort" at "presort.xqm";
24
25
import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm";

MRodz's avatar
MRodz committed
26

27
declare variable $tidySimple:valid-hands :=
28
29
    for $res in collection("/db/sade-projects/textgrid/data/xml/data")
(:    for $res in collection("/db/apps/SADE/resources/xml"):)
MRodz's avatar
MRodz committed
30
31
32
33
    return
        $res//tei:handNote[@script = "contemporary"]/@xml:id/string();


34
declare function tidySimple:main($tei as node()*) {
MRodz's avatar
MRodz committed
35
36
37
    let $tidy := tidySimple:sort-out-invalid-hands($tei//tei:text) 
        => tidySimple:sort-out-surplus-elements()
        => tidySimple:whitespaces()
38
        => tidySimple:summarize-entries()
39
        => tidySimple:split-headings()
40
(:        => tidySimple:sort():)
MRodz's avatar
MRodz committed
41
(:    let $text-with-sections := tidySimple:make-structure($clear-surplus-hands):)
MRodz's avatar
MRodz committed
42
    let $header := $tei/tei:teiHeader
MRodz's avatar
MRodz committed
43
    return 
mrodzis's avatar
mrodzis committed
44
        xmldb:store("/db/apps/SADE/resources/xml/", "fontane-tei.xml", <TEI xmlns="http://www.tei-c.org/ns/1.0" id="{$tei//tei:TEI/@id}">{$header}{$tidy}</TEI>)
MRodz's avatar
MRodz committed
45
46
};

47
48
49
50
51
declare function tidySimple:sort-out-invalid-hands($nodes as node()*) 
as node()* {
    for $node in $nodes return
        typeswitch ($node)
        case text() return
MRodz's avatar
MRodz committed
52
53
            if($node/preceding::tei:milestone[@unit = "handshift"][1] 
            and not(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $node/preceding::tei:milestone[@unit = "handshift"][1]))) then
MRodz's avatar
MRodz committed
54
55
56
                ()
            else
                $node
57
        
58
59
60
61
        (: all lines have to be preserved because of the editorial commentary
        which references the lines in the notebooks. if we omitted @unit = "line"
        referencing wouldn't work any longer :)        
        case element(tei:milestone) return
MRodz's avatar
MRodz committed
62
            let $prev-handshift := $node/preceding::tei:milestone[@unit = "handshift"][1]
63
                return
MRodz's avatar
MRodz committed
64
                if($node/@unit = "handshift" and
65
                simpleHelpers:is-hand-valid($tidySimple:valid-hands, $node)) then
66
67
                    tidySimple:clear-handshift($node)  
                    
MRodz's avatar
MRodz committed
68
                else if($node/@unit = "handshift") then
69
70
71
72
73
74
75
76
77
78
79
80
81
                    ()
                    
                else if($prev-handshift
                and $node/@unit = "line" 
                and not(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $prev-handshift))) then
                    tidySimple:copy-element($node, "post")
                
                else if($prev-handshift
                and not(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $prev-handshift))) then
                    ()
                
                else
                    tidySimple:copy-element($node, "post")
82
83
                
        default return
MRodz's avatar
MRodz committed
84
            let $prev-handshift := $node/preceding::tei:milestone[@unit = "handshift"][1]
85
86
87
88
            let $first-child-handshift := $node/child::tei:milestone[@unit = "handshift"][1]
            let $first-child-element := $node/child::*[1]
            let $first-child-node := $node/child::node()[1]
            
89
            return
90
91
92
93
94
95
96
97
98
99
100
                (: in some cases the valid handshift is the first child node 
                instead of a previous node. of course we want to keep the element
                then :)
                if($first-child-element = $first-child-handshift
                (: ensure there's no text before the handshift :)
                and (normalize-space($first-child-node) = ""
                    or $first-child-element = $first-child-node)
                and simpleHelpers:is-hand-valid($tidySimple:valid-hands, $first-child-handshift)) then
                    tidySimple:copy-element($node, "post")
                
                else if($prev-handshift
101
102
                and not(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $prev-handshift))) then
                    ()
103
                    
104
105
                else
                    tidySimple:copy-element($node, "post")
106
107
108
};


109
declare function tidySimple:sort-out-surplus-elements($nodes as node()*) 
110
111
112
113
114
115
as node()* {
    for $node in $nodes return
        typeswitch ($node)
        case text() return
            $node
        
116
        case element(tei:milestone) return
MRodz's avatar
MRodz committed
117
            if($node/@unit = "handshift") then
118
119
120
121
122
                if(simpleHelpers:is-prev-hand-same($node)
                or not(tidySimple:has-hand-text($node))) then
                    ()
                else
                    tidySimple:clear-handshift($node)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
                    
            else if($node/@unit = "line") then
                let $next-pb := $node/following::tei:pb[1]
                let $nodes-on-page := $node/preceding::tei:pb[1]/following::node()[. << $next-pb][functx:node-kind(.) = "text"]
                let $contains-text :=
                    for $node-pb in $nodes-on-page return
                        if(normalize-space($node-pb) != "") then
                            true()
                        else 
                            false()
                return
                    if(functx:is-value-in-sequence(true(), $contains-text)) then
                        element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                            $node/@*,
                            tidySimple:sort-out-surplus-elements($node/node())
                        }
                    else
                        ()
                
142
            else
143
144
                element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                    $node/@*,
145
                    tidySimple:sort-out-surplus-elements($node/node())
146
                }                
147
148
                
        default return
149
150
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                $node/@*,
151
                tidySimple:sort-out-surplus-elements($node/node())
152
            }
153
154
};

155
declare function tidySimple:has-hand-text($node as element(tei:milestone))
156
as xs:boolean {
MRodz's avatar
MRodz committed
157
    let $next-handshift := $node/following::tei:milestone[@unit = "handshift"][1]
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    let $nodes-between := $node/following::node()[. << $next-handshift]
    let $is-text-node :=
        for $node-between in $nodes-between
        return 
            if ($node-between[self::text()]
            and not(normalize-space($node-between) = "")) then
                true()
            else
                false()
    return 
        if($next-handshift 
        and functx:is-value-in-sequence(true(), $is-text-node)) then
            true()
        else if(not($next-handshift)) then
            true()
        else
            false()
};

MRodz's avatar
MRodz committed
177

178
declare function tidySimple:copy-element($node as node(), $flag as xs:string)
179
 {
180
181
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
        $node/@*,
182
183
        if($flag = "structure") then
            tidySimple:make-structure($node/node())
184
185
186
        else if($flag = "post") then
            tidySimple:sort-out-invalid-hands($node/node())        
        else if($flag = "surplus") then
187
188
189
            tidySimple:sort-out-invalid-hands($node/node())        
        else if($flag = "summarize") then
            tidySimple:summarize-entries($node/node())
190
191
        else
            tidySimple:tidy($node/node())
192
193
194
    }
};

195
(:~
MRodz's avatar
MRodz committed
196
 : Purges surplus attributes from tei:milestone[@unit = "handshift"].
197
 : 
MRodz's avatar
MRodz committed
198
 : @author Michelle Weidling
MRodz's avatar
MRodz committed
199
200
 : @param $node the current tei:milestone[@unit = "handshift"]
 : @return the purged tei:milestone[@unit = "handshift"]
201
 :  :)
202
203
declare function tidySimple:clear-handshift($node as element(tei:milestone))
as element(tei:milestone) {
MRodz's avatar
MRodz committed
204
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
MRodz's avatar
MRodz committed
205
        attribute unit {"handshift"},
206
207
        $node/@ws-before,
        $node/@ws-after,
208
        if($node/@subtype = "") then
209
210
            ()
        else
211
212
            $node/@subtype,
        if($node/@rend = "") then
213
214
            ()
        else
215
            $node/@rend
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
(:~ 
 : 
 :  :)
declare function tidySimple:make-structure($nodes as node()*) as node()* {
    for $node in $nodes return
        typeswitch ($node)
        case text() return
            if(tidySimple:is-in-section($node)) then
                ()
            else
                $node
                
        case element(tei:milestone) return 
            if($node/@unit = "section") then
                tidySimple:make-section($node)
                
            else if($node/@unit = "paragraph") then
                tidySimple:copy-element($node, "structure")
            
            (: lines in sections have to be ignored since they are copied into 
            the section with get-section-chunk :)    
            else if($node/@unit = "line"
            and tidySimple:is-in-section($node)) then
                ()
            else
                tidySimple:copy-element($node, "structure")
                
        case element(tei:anchor) return
            if (matches($node/preceding::tei:milestone/@spanTo, $node/@xml:id)) then
                ()
            else
                tidySimple:copy-element($node, "structure")
                
        default return 
            if(tidySimple:is-in-section($node)) then
                ()
            else
                tidySimple:copy-element($node, "structure")
};
259

260
261
262
263
(:~
 : Checks if a given node is between a tei:milestone[@unit = "section"] and its
 : corresponing tei:anchor.
 : 
MRodz's avatar
MRodz committed
264
 : @author Michelle Weidling
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 : @param the current node
 : @return xs:boolean
 :  :)
declare function tidySimple:is-in-section($node as node()) as xs:boolean {
    let $prev-section-marker := $node/preceding::tei:milestone[1][@spanTo]
    let $target-id := replace($prev-section-marker, "#", "")
    let $next-anchor := $node/following::tei:anchor[matches(@xml:id, $target-id)]
    return
        if($prev-section-marker 
        and $next-anchor) then
            true()
        else
            false()
};


(:~
 : Creates a tei:div from a given tei:milestone[@unit = "section"] that contains 
 : all elements from a given tei:milestone to its corresponding tei:anchor.
 : 
 : Since the nodes between the current and the target one may also contain 
 : structural information (e.g. tei:milestone[@unit = "paragraph"]), these nodes
 : cannot simply be copied but have to be transformed as well.
 : 
MRodz's avatar
MRodz committed
289
 : @author Michelle Weidling
290
291
292
 : @param $node the current tei:milestone[@unit = "section"]
 : @return element(tei:div)
 :  :)
293
294
declare function tidySimple:make-section($node as element(tei:milestone)) 
as element(tei:div) {
MRodz's avatar
MRodz committed
295
    element tei:div {
296
297
298
299
300
301
302
303
304
305
        attribute type {"section"},
        tidySimple:get-section-chunk($node)
    }
};


(:~
 : Returns all nodes between a tei:milestone[@unit = "section"] and its 
 : corresponding tei:anchor.
 :
MRodz's avatar
MRodz committed
306
 : @author Michelle Weidling
307
308
309
310
311
312
313
314
 : @param the current tei:milestone element
 : @return all nodes between the current element and the respective tei:anchor
 :)
declare function tidySimple:get-section-chunk($node as element(tei:milestone)) 
as node()* {
    let $target-id := substring-after($node/@spanTo, "#")
    let $target := $node/following::tei:anchor[matches(@xml:id, $target-id)]
    return
315
        $node/following-sibling::node()[. << $target]
316
317
318
};


319
320
declare function tidySimple:tidy($nodes as node()*) as node()* {
    ()
MRodz's avatar
MRodz committed
321
322
};

MRodz's avatar
MRodz committed
323
declare function tidySimple:whitespaces($nodes as node()*) as node()* {
MRodz's avatar
MRodz committed
324
325
326
327
328
    for $node in $nodes return
        typeswitch($node)
        case text() return
            $node
            
MRodz's avatar
MRodz committed
329
330
331
332
333
        default return
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                $node/@*,
                if($node/@next) then
                    let $target-id := replace($node/@next, "#", "")
334
                    let $target := $node/ancestor::tei:TEI//*[@xml:id = $target-id]
MRodz's avatar
MRodz committed
335
                    return
336
337
338
                        if(ends-with($node//text()[last()], "@P")
                        (: example: C6 Kapell:drf. :)
                        or $node[self::tei:abbr][ends-with(., ":")]
339
340
                        or $node[self::tei:rs]/tei:choice[not(following-sibling::*)][tei:abbr[ends-with(., ":")]]
                        or $node[ends-with(., "-")]) then
mrodzis's avatar
mrodzis committed
341
                            ()
342
343
                        else
                            attribute break {"true"}
MRodz's avatar
MRodz committed
344
345
346
347
                else
                    (),
                tidySimple:whitespaces($node/node())
            }
MRodz's avatar
MRodz committed
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
declare function tidySimple:split-headings($nodes as node()*) as node()* {
    for $node in $nodes return
        typeswitch($node)
        case text() return
            $node
        
        case element(tei:head) return
            if($node//tei:lb[@type = "edited_text"]) then
                let $lb := $node//tei:lb[@type = "edited_text"]
                let $split-top := $node/node()[. << $lb]
                let $split-bottom := $node/node()[. >> $lb]
                return
                    element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                        $node/@*,
                        (element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                            attribute type {"upper-part"},
                            $split-top
                        },
                        element {QName("http://www.tei-c.org/ns/1.0", "lb")} {
                            attribute type {"edited_text"}
                        },
                        element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                            attribute type {"lower-part"},
                            $split-bottom
                        })
                    }
mrodzis's avatar
mrodzis committed
377
                    
378
379
380
381
382
383
384
385
386
            else
                element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                    $node/@*,
                    tidySimple:split-headings($node/node())
                }
            
        default return
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                $node/@*,
mrodzis's avatar
mrodzis committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
                if($node/tei:add[@type = "edited_text" and @subtype = "interlinear"]) then
                    let $add := $node/tei:add[@type = "edited_text" and @subtype = "interlinear"]
                    return
                        (element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                            attribute type {"upper-part"},
                            $add/node()
                        },
                        element {QName("http://www.tei-c.org/ns/1.0", "lb")} {
                            attribute type {"edited_text"}
                        },
                        element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                            attribute type {"lower-part"},
                            $add/following-sibling::node()
                        })
                else
                    tidySimple:split-headings($node/node())
403
            }
404
405
406
407
408
409
410
411
412
413
};

declare function tidySimple:summarize-entries($nodes as node()*) as node()* {
    for $node in $nodes return
        typeswitch ($node)
        
        case text() return
            $node
        
        case element(tei:hi) return
414
            if($node/@next and not($node/@prev)) then
415
                let $this-content := $node/child::text()
416

417
418
                return
                    element {QName("http://www.tei-c.org/ns/1.0", "hi")} {
419
420
                        $this-content,
                        tidySimple:get-next-content($node, ())
421
422
423
424
425
426
427
428
                    }
            else if($node[@prev]) then
                ()
            else
                 tidySimple:copy-element($node, "summarize")
                 
        default return
            tidySimple:copy-element($node, "summarize")
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
};

declare function tidySimple:get-next-content($node as node()*, $content as node()*) 
as node()* {
    if($node/@next) then
        let $next-hi-id := substring-after($node/@next, "#")
        let $next-node := $node/following-sibling::*[@xml:id = $next-hi-id]
        let $next-content := $next-node/text()
        let $break := if($node/@break = "true") then text{" "} else ()
        let $content := ($content, $break, $next-content)

        return 
            tidySimple:get-next-content($next-node, $content)
    else
        $content
444
};