tidysimple.xqm 2.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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";

declare namespace tei="http://www.tei-c.org/ns/1.0";

MRodz's avatar
MRodz committed
18
import module namespace config="http://textgrid.de/ns/SADE/config" at "../../config/config.xqm";
19
20
21
import module namespace console="http://exist-db.org/xquery/console";
import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm";

MRodz's avatar
MRodz committed
22

23
declare variable $tidySimple:valid-hands :=
MRodz's avatar
MRodz committed
24
25
26
27
28
    for $res in collection("/db/sade-projects/textgrid/data/xml/data")
    return
        $res//tei:handNote[@script = "contemporary"]/@xml:id/string();


29
declare function tidySimple:main() as xs:string* {
MRodz's avatar
MRodz committed
30
31
32
33
34
35
36
37
38
  let $doc :=
    try {
      (doc($config:app-root || "/resources/xml/tei-simple-tmp.xml"))
    } catch * {
      (console:log("It was not possible to open the requested file tei-simple-tmp.xml."))
    }

    let $text := $doc//tei:text
    let $tidied-text := tidySimple:tidy($text)
39
    let $final-tei:=
MRodz's avatar
MRodz committed
40
41
42
43
        <TEI xmlns="http://www.tei-c.org/ns/1.0">
            {$doc//tei:teiHeader}
            {$tidied-text}
        </TEI>
44

MRodz's avatar
MRodz committed
45
46
47
48
49
50
51
52
53
54
    return xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple.xml", $final-tei)
};


declare function tidySimple:tidy($nodes as node()*) as node()* {
    for $node in $nodes return
        typeswitch ($node)
        case text() return
            $node
        case element(tei:handShift) return
MRodz's avatar
MRodz committed
55
            if(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $node)
56
57
            and not(simpleHelpers:is-prev-valid-hand-same($tidySimple:valid-hands, $node))) then
                tidySimple:clear-handshift($node)
MRodz's avatar
MRodz committed
58
59
60
            else
                ()
        default return
61
            if(simpleHelpers:belongs-to-valid-hand($tidySimple:valid-hands, $node)) then
62
                tidySimple:copy-second-stage-element($node)
63
64
            else
                ()
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
};


declare function tidySimple:copy-second-stage-element($node as node())
as node() {
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
        $node/@*,
        tidySimple:tidy($node/node())
    }
};


declare function tidySimple:clear-handshift($node as element(tei:handShift))
as element(tei:handShift) {
    element tei:handShift {
        if($node/@new = "") then
            ()
        else
            $node/@new,
        if($node/@script = "") then
            ()
        else
            $node/@script,
        if($node/@medium = "") then
            ()
        else
            $node/@medium
    }
};