tidysimple.xqm 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.
 :
 : @author Michelle Rodzis
 : @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";
23
24
import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm";

MRodz's avatar
MRodz committed
25

26
declare variable $tidySimple:valid-hands :=
27
28
    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
29
30
31
32
    return
        $res//tei:handNote[@script = "contemporary"]/@xml:id/string();


33
declare function tidySimple:main($tei as node()*) {
34
35
36
37
38
39
40
  let $doc :=
    try {
      (doc($config:app-root || "/resources/xml/tei-simple-pre.xml"))
    } catch * {
      (console:log("It was not possible to open the requested file tei-simple-pre.xml."))
    }
    let $text := $doc//tei:text
41
42
43
    let $clear-invalid-hands := tidySimple:sort-out-invalid-hands($text)
    let $clear-surplus-hands := tidySimple:sort-out-surplus-hands($clear-invalid-hands)
    let $text-with-sections := tidySimple:make-structure($clear-surplus-hands)
44
    let $store := xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple-tmp.xml", <div>{$text-with-sections}</div>)
45
    return element tei:div {$text-with-sections}
46
47
48
49
50
51
52
53
54
(:    let $doc := doc($config:app-root || "/resources/xml/tei-simple-tmp.xml"):)
(:    let $tidied-text := tidySimple:tidy($doc//tei:text):)
(:    let $final-tei:=:)
(:        <TEI xmlns="http://www.tei-c.org/ns/1.0">:)
(:            {$tei//tei:teiHeader}:)
(:            {$tidied-text}:)
(:        </TEI>:)
(::)
(:    return xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple.xml", $final-tei):)
MRodz's avatar
MRodz committed
55
56
};

57
58
59
60
61
declare function tidySimple:sort-out-invalid-hands($nodes as node()*) 
as node()* {
    for $node in $nodes return
        typeswitch ($node)
        case text() return
62
63
            if($node/preceding::tei:milestone[@type = "handshift"][1] 
            and not(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $node/preceding::tei:milestone[@type = "handshift"][1]))) then
MRodz's avatar
MRodz committed
64
65
66
                ()
            else
                $node
67
        
68
69
70
71
        (: 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
            let $prev-handshift := $node/preceding::tei:milestone[@type = "handshift"][1]
                return
                if($node/@type = "handshift" and
                    simpleHelpers:is-hand-valid($tidySimple:valid-hands, $node)) then
                    tidySimple:clear-handshift($node)  
                    
                else if($node/@type = "handshift") then
                    ()
                    
                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")
92
93
                
        default return
94
95
96
97
98
99
100
            let $prev-handshift := $node/preceding::tei:milestone[@type = "handshift"][1]
            return
                if($prev-handshift
                and not(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $prev-handshift))) then
                    ()
                else
                    tidySimple:copy-element($node, "post")
101
102
103
104
105
106
107
108
109
110
};


declare function tidySimple:sort-out-surplus-hands($nodes as node()*) 
as node()* {
    for $node in $nodes return
        typeswitch ($node)
        case text() return
            $node
        
111
112
113
114
115
116
117
        case element(tei:milestone) return
            if($node/@type ="handshift") then
                if(simpleHelpers:is-prev-hand-same($node)
                or not(tidySimple:has-hand-text($node))) then
                    ()
                else
                    tidySimple:clear-handshift($node)
118
            else
119
120
121
122
                element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                    $node/@*,
                    tidySimple:sort-out-surplus-hands($node/node())
                }                
123
124
                
        default return
125
126
127
128
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
                $node/@*,
                tidySimple:sort-out-surplus-hands($node/node())
            }
129
130
};

131
declare function tidySimple:has-hand-text($node as element(tei:milestone))
132
as xs:boolean {
133
    let $next-handshift := $node/following::tei:milestone[@type = "handshift"][1]
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    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
153

154
declare function tidySimple:copy-element($node as node(), $flag as xs:string)
155
 {
156
157
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
        $node/@*,
158
159
        if($flag = "structure") then
            tidySimple:make-structure($node/node())
160
161
162
163
        else if($flag = "post") then
            tidySimple:sort-out-invalid-hands($node/node())        
        else if($flag = "surplus") then
            tidySimple:sort-out-invalid-hands($node/node())
164
165
        else
            tidySimple:tidy($node/node())
166
167
168
    }
};

169
(:~
170
 : Purges surplus attributes from tei:milestone[@type = "handshift"].
171
172
 : 
 : @author Michelle Rodzis
173
174
 : @param $node the current tei:milestone[@type = "handshift"]
 : @return the purged tei:milestone[@type = "handshift"]
175
 :  :)
176
177
178
179
180
declare function tidySimple:clear-handshift($node as element(tei:milestone))
as element(tei:milestone) {
    element tei:milestone {
        attribute type {"handshift"},
        if($node/@subtype = "") then
181
182
            ()
        else
183
184
            $node/@subtype,
        if($node/@rend = "") then
185
186
            ()
        else
187
            $node/@rend
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
(:~ 
 : 
 :  :)
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")
};
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
(:~
 : Checks if a given node is between a tei:milestone[@unit = "section"] and its
 : corresponing tei:anchor.
 : 
 : @author Michelle Rodzis
 : @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.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:milestone[@unit = "section"]
 : @return element(tei:div)
 :  :)
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
declare function tidySimple:make-section($node as element(tei:milestone)) 
as element(tei:div) {
    element tei:div {
        attribute type {"section"},
        tidySimple:get-section-chunk($node)
    }
};


(:~
 : Returns all nodes between a tei:milestone[@unit = "section"] and its 
 : corresponding tei:anchor.
 :
 : @author Michelle Rodzis
 : @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
287
        $node/following-sibling::node()[. << $target]
288
289
290
};


291
292
declare function tidySimple:tidy($nodes as node()*) as node()* {
    ()
293
};