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

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

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";
9
import module namespace functx="http://www.functx.com";
10
11
import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm";

12
13
14
15
declare function presort:main($tei as node()*) as element(tei:text) {
    let $front := $tei//tei:front/node()
    let $back := $tei//tei:back/node()
    let $body := $tei//tei:body/node()
16

17
18
19
20
21
    let $tei :=
        <text xmlns="http://www.tei-c.org/ns/1.0">
            <front>{presort:sort($front)}</front>
            <body>{presort:sort($body)}</body>
            <back>{presort:sort($back)}</back>
22
        </text>
23
    let $store := xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple-presort.xml", $tei)
24
25
26
27
28
29
30
    return $tei
};

declare function presort:sort($nodes as node()*) as node()* {
    for $node in $nodes
      return
        typeswitch ($node)
MRodz's avatar
MRodz committed
31
        case text() return 
32
            $node
MRodz's avatar
MRodz committed
33
34
35
            
        case comment() return
            ()
36
37
38
            
        case element(tei:rs) return
            presort:exclude-copied($node)
39
40
        
        default return
41
42
43
44
45
46
47
48
49
50
51
52
            if($node/@next and not($node/@prev)) then
                (presort:apply-all-nexts($node))
            else if($node/@prev or $node/@next) then
                let $prev-id := presort:get-prev-id($node)
                let $next-id := presort:get-next-id($node)
                let $prev-node := presort:find-corresp-node($node, $prev-id)
                let $next-node := presort:find-corresp-node($node, $next-id)
                return
                    if($prev-node or $next-node) then
                        ()
                    else
                        presort:keep-node($node)
MRodz's avatar
MRodz committed
53
            else
54
                presort:keep-node($node)
MRodz's avatar
MRodz committed
55
56
57
};

declare function presort:keep-node($node as node()*) as node()* {
58
59
60
61
62
    if($node[self::text()]) then
        util:log-system-out("text")
    else
        (),
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
MRodz's avatar
MRodz committed
63
        $node/@*,
64
(:        $node/(@* except (@prev, @next)), (: attrs only visible for debugging :):)
MRodz's avatar
MRodz committed
65
66
67
68
        presort:sort($node/node())
    }
};

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
94
95
96
97
98

declare function presort:apply-all-nexts($node as node()*) {
    (: entry point :)
    if($node/@next and not($node/@prev)) then
        let $next-id := presort:get-next-id($node)
        let $next-node := presort:find-corresp-node($node, $next-id)
        return 
            (presort:keep-node($node),
(:            (functx:remove-attributes($node, "next"),:)
            presort:apply-all-nexts($next-node))
    (: exit point :)
    else if(not($node/@next)) then
(:        functx:remove-attributes($node, "prev"):)
        presort:keep-node($node)
    else
        let $next-id := presort:get-next-id($node)
        let $next-node := presort:find-corresp-node($node, $next-id)
        return
(:            (functx:remove-attributes($node, ("next", "prev")),:)
            (presort:keep-node($node),
            presort:apply-all-nexts($next-node))
};


declare function presort:get-next-id($node as node()) {
    $node/@next => replace("#", "")
};

declare function presort:get-prev-id($node as node()) {
    $node/@prev => replace("#", "")
MRodz's avatar
MRodz committed
99
100
101
102
103
104
};

declare function presort:find-corresp-node($node as node()*, $id as xs:string) {
    $node/ancestor::*[last()]//*[@xml:id = $id]
};

105
106
107
108
109
declare function presort:exclude-copied($node as node()) {
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
        $node/@*,
        $node/node()[not(@prev or @next)]
    }
110
};