transform2teisimple.xqm 7.56 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 : 
 : 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";

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?
 : 
 : :)
declare function fontane-simple:main($file as xs:string) as element(tei:TEI) {
  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
37
38
39
40
41
42
43
    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"))]
    
    return 
    <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
51
52
      </TEI>
};

MRodz's avatar
MRodz committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(:~ 
 : 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
67
68
69
70
71
72
    for $node in $nodes
      return
        typeswitch ($node)
        case text()
          return
            $node
MRodz's avatar
MRodz committed
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
        case element(tei:graphic)
            return
                element{"tei:graphic"}{
                    $node/@*
                }
        case element(tei:del)
            return
                ()
        case element(tei:fw)
            return
                ()
        case element(tei:line)
            return
                if(count($node/*) = 1 and $node/child::tei:fw)
                    then
                        ()
                    else
                        fontane-simple:transform($node/node())
        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
                        element{"tei:handShift"}{
                            $node/@*
                        }
                    else
                        ()
                
(:                if($node/following-sibling::*[1][count(./*) = 1 :)
(:                    and child::tei:fw or child::tei:stamp]):)
(:                    then:)
(:                        ():)
(:                    else:)
(:                        element{"tei:handShift"}{:)
(:                            $node/@*:)
(:                        }:)
        case element(tei:stamp)
            return
                ()
        case element(tei:seg)
            return
                if(count($node/*) = 1 and $node/child::tei:stamp)
                    then
                        ()
                else if($node/@type = "initials" or $node/@type = "monogram" or
                    $node/@style)
                    then
                        element{"tei:seg"}{
                            $node/@*,
                            fontane-simple:transform($node/node())
                        }
                else if($node/@type = "auction_number")
                    then
                        ()
                    else
                        fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
132
        case element(tei:surface)
MRodz's avatar
MRodz committed
133
            return 
MRodz's avatar
MRodz committed
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
                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{"facs"}{$node/@facs},
                            attribute{"xml:id"}{$node/@xml:id},
                            attribute{"type"}{$node/@n},
                            fontane-simple:transform($node/node())
                        })
                else if(matches($node/@n, "[0-9]{1,4}[rv]{1}"))
                    then
                        (element{"tei:pb"}{
                            attribute{"n"}{$node/@n},
                            if($node/@facs)
                                then
                                    attribute{"facs"}{$node/@facs}
                                else
                                    ()
                        }, 
                        (: if all descendant text nodes shoudln't be serialized, z.g. parent::fw or parent::stamp :)
                        if($node)
                            then
                                element{"tei:div"}{
                                    attribute{"type"}{"page"},
                                    fontane-simple:transform($node/node())
                                }
                            else
                                ())
                else if($node/@type = "label" and
MRodz's avatar
MRodz committed
174
175
176
177
178
179
180
181
                    (contains($node/@subtype, "Fontane")
                    or contains($node/@subtype, "Hersteller"))    
                )
                    then
                        element{"tei:div"}{
                            attribute{"type"}{"label"},
                            fontane-simple:transform($node/node())
                        }
MRodz's avatar
MRodz committed
182
183
184
185
                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
186
187
                    else
                        ()
MRodz's avatar
MRodz committed
188
        default return
MRodz's avatar
MRodz committed
189
          fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
190
191
192
193
194
195
196
197
198
199
200
201
202
};

(: 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()
MRodz's avatar
MRodz committed
203
};