transform.xqm 29.3 KB
Newer Older
mmarkus1's avatar
mmarkus1 committed
1
xquery version "3.1";
mmarkus1's avatar
bugfix    
mmarkus1 committed
2
3
4
5
6
7

module namespace transformXML="https://sade.textgrid.de/ns/transform";
import module namespace code-view="http://bdn-edition.de/ns/code-view" at "/db/apps/code-view/content/main.xqm";

declare boundary-space strip;
declare namespace tei="http://www.tei-c.org/ns/1.0";
mmarkus1's avatar
mmarkus1 committed
8
declare namespace xhtml="http://www.w3.org/1999/xhtml";
mmarkus1's avatar
bugfix    
mmarkus1 committed
9
10
11
declare namespace tg="http://textgrid.info/namespaces/metadata/core/2010";
declare namespace functx = "http://www.functx.com";
declare default element namespace "http://www.w3.org/1999/xhtml";
mmarkus1's avatar
mmarkus1 committed
12

mmarkus1's avatar
bugfix    
mmarkus1 committed
13
14
15
declare function transformXML:importEdition($tguri as xs:string) {
(:getEdition itself and store in html pages:)
(:    transformXML:storeInPages('34zmq','34zmq'):)
mmarkus1's avatar
mmarkus1 committed
16

mmarkus1's avatar
bugfix    
mmarkus1 committed
17
(:getManifest?:)
mmarkus1's avatar
mmarkus1 committed
18
19


mmarkus1's avatar
bugfix    
mmarkus1 committed
20
(:getEdition and prepare xml files in pages? Module&Function still missing:)
mmarkus1's avatar
mmarkus1 committed
21

mmarkus1's avatar
bugfix    
mmarkus1 committed
22
23
24
let $output := 'done'
return $output
};
mmarkus1's avatar
mmarkus1 committed
25

mmarkus1's avatar
bugfix    
mmarkus1 committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
declare function functx:reverse-string ( $arg as xs:string? )  as xs:string {
   codepoints-to-string(reverse(string-to-codepoints($arg)))
};
declare function functx:repeat-string( $stringToRepeat as xs:string?,$count as xs:integer )  as xs:string {

   string-join((for $i in 1 to $count return $stringToRepeat),
                        '')
 } ;
declare function functx:pad-integer-to-length( $integerToPad as xs:anyAtomicType? , $length as xs:integer )  as xs:string {

   if ($length < string-length(string($integerToPad)))
   then error(xs:QName('functx:Integer_Longer_Than_Length'))
   else concat
         (functx:repeat-string(
            '0',$length - string-length(string($integerToPad))),
          string($integerToPad))
 } ;
declare function functx:date( $year as xs:anyAtomicType,$month as xs:anyAtomicType,$day as xs:anyAtomicType ) as xs:date {
xs:date(
 concat(
   functx:pad-integer-to-length(xs:integer($year),4),'-',
   functx:pad-integer-to-length(xs:integer($month),2),'-',
   functx:pad-integer-to-length(xs:integer($day),2)))
};
mmarkus1's avatar
mmarkus1 committed
50

mmarkus1's avatar
bugfix    
mmarkus1 committed
51
52
53
54
55
56
57
58
59
declare function local:castDate($string) {
    let $result := tokenize($string, '-')
    let $count := count($result)
    return
        if ($count = 1) then (
            let $output := $result[1]
            return $output
        )
        else if ($count = 2) then (
mmarkus1's avatar
mmarkus1 committed
60
            let $output := string-join(($result[2],$result[1]),'-')
mmarkus1's avatar
bugfix    
mmarkus1 committed
61
62
63
64
65
66
67
68
69
70
71
            return $output
        )
        else if ($count = 3) then (
            let $date := functx:date($result[1],$result[2],$result[3])
            let $ouput := string-join((fn:day-from-date($date),fn:month-from-date($date),fn:year-from-date($date)),'-')
            return $ouput
        )
        else (
            let $output := "error: invalid date"
            return $output
        )
72

mmarkus1's avatar
mmarkus1 committed
73
74
};

mmarkus1's avatar
bugfix    
mmarkus1 committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
declare function local:milestone-chunk-ns(
  $ms1 as element(),
  $ms2 as element(),
  $node as node()*
) as node()*
{
  typeswitch ($node)
    case element() return
      if ($node is $ms1) then $node
      else if ( some $n in $node/descendant::* satisfies ($n is $ms1 or $n is $ms2) )
      then
        (: element { name($node) } :)
           element {QName (namespace-uri($node), name($node))}
                { for $i in ( $node/node() | $node/@* )
                  return local:milestone-chunk-ns($ms1, $ms2, $i) }
      else if ( $node >> $ms1 and $node << $ms2 ) then $node
      else ()
    case attribute() return $node (: will never match attributes outside non-returned elements :)
93
    default return
mmarkus1's avatar
bugfix    
mmarkus1 committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      if ( $node >> $ms1 and $node << $ms2 ) then $node
      else ()
};
declare function local:milestone-chunk-ns-last(
  $ms1 as element(),
  $ms2 as element(),
  $node as node()*
) as node()*
{
  typeswitch ($node)
    case element() return
      if ($node is $ms1) then $node
      else if ( some $n in $node/descendant::* satisfies ($n is $ms1 or $n is $ms2) )
      then
        (: element { name($node) } :)
           element {QName (namespace-uri($node), name($node))}
                { for $i in ( $node/node() | $node/@* )
                  return local:milestone-chunk-ns-last($ms1, $ms2, $i) }
      else if ( $node >> $ms1 and $node << $ms2 ) then ($node)
      else if ($node is $ms2) then ( $ms2 )
      else ()
    case attribute() return $node (: will never match attributes outside non-returned elements :)
116
    default return
mmarkus1's avatar
bugfix    
mmarkus1 committed
117
118
      if ( $node >> $ms1 and $node << $ms2 ) then $node
      else ()
mmarkus1's avatar
mmarkus1 committed
119
120
};

mmarkus1's avatar
bugfix    
mmarkus1 committed
121
122
123
declare function local:countPB($length) {
    let $pbNumbers := for $i in 1 to $length
    return $i
124

mmarkus1's avatar
bugfix    
mmarkus1 committed
125
    return string-join($pbNumbers,',')
mmarkus1's avatar
mmarkus1 committed
126
127
};

mmarkus1's avatar
bugfix    
mmarkus1 committed
128
129
130
declare function local:countFW($length,$body) {
    let $fwNumbers := for $i in 1 to $length
    return $body//tei:pb[@n=$i]/following::tei:fw[1]/node()
131

mmarkus1's avatar
bugfix    
mmarkus1 committed
132
    return string-join($fwNumbers,',')
mmarkus1's avatar
mmarkus1 committed
133
134
};

mmarkus1's avatar
bugfix    
mmarkus1 committed
135
136
137
138
declare function transformXML:importFromTGRep($tgURI as xs:string, $filenameOutput as xs:string) {
    let $login := xmldb:login("/db", "admin", "" )
    let $pathInput := 'https://textgridlab.org/1.0/tgcrud-public/rest/textgrid:'
    let $pathInputSuffix := '/data'
139
    let $pathInputSuffixForMetadata := '/metadata'
mmarkus1's avatar
bugfix    
mmarkus1 committed
140
141
    let $doc := doc($pathInput || $tgURI || $pathInputSuffix)
    let $docForHeader := doc($pathInput || $tgURI || $pathInputSuffix)
142
    let $docForMetadata := doc($pathInput || $tgURI || $pathInputSuffixForMetadata)
mmarkus1's avatar
bugfix    
mmarkus1 committed
143
144
145
146
    let $pathOutput := '/db/apps/sade-architrave/templates/'|| $filenameOutput

    let $header := transformXML:magicForHeader($docForHeader//tei:TEI/*)
    let $metadata := local:magicForMetadata($docForMetadata/*)
147
148
149
150
151
152

    let $input := $doc//tei:TEI/tei:text//tei:body
    let $counterPages := count($input//tei:pb)
    let $pbNumbers as xs:string := local:countPB($counterPages)
    let $fwNumbers as xs:string := local:countFW($counterPages,$input)

mmarkus1's avatar
mmarkus1 committed
153
154
    let $tgURIshort := substring-before($tgURI,'.')
    let $storeInDB := xmldb:store('/db/apps/sade-architrave/textgrid/data/', $tgURIshort || '.xml' , $input )
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    for $i in 1 to $counterPages
            return
                if ($i < $counterPages) then (
                    let $first := $i
                    let $next := $i+1
                    let $output := local:milestone-chunk-ns($input//tei:pb[@n=string($first)],$input//tei:pb[@n=string($next)],$input)
                    let $outputXML := code-view:main($output)
                    let $output := local:magic($output)
                    let $filenameOutputPage := $filenameOutput || '_p' || string($i)
                    let $output := <div>{$metadata}<div id="tei-header"><span style="display:none" id="pbNumbers">{$pbNumbers}</span><span style="display:none" id="fwNumbers">{$fwNumbers}</span>{$header}</div>{$output}</div>
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '.html' , $output )
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '_xml.html' , $outputXML )
                    return $output
                )
                else (
                    let $output := local:milestone-chunk-ns-last($input//tei:pb[@n=string($counterPages)], ($input//tei:pb[@n="1"]/following-sibling::*[last()]),$input)
                    let $outputXML := code-view:main($output)
                    let $output := local:magic($output)
                    let $filenameOutputPage := $filenameOutput || '_p' || string($i)
                    let $output := <div>{$metadata}<div id="tei-header"><span style="display:none" id="pbNumbers">{$pbNumbers}</span><span style="display:none" id="fwNumbers">{$fwNumbers}</span>{$header}</div>{$output}</div>
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '.html' , $output )
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '_xml.html' , $outputXML )
                    return $output
                )
};

declare function transformXML:importFromTGLab($tgURI as xs:string) {
    let $login := xmldb:login("/db", "admin", "" )
    let $pathInput := '/db/apps/sade-architrave/textgrid/data/'
    let $pathInputMetadata := '/db/apps/sade-architrave/textgrid/meta/'
    let $filenameOutput := $tgURI
    let $pathInputSuffix := '.xml'
    let $pathInputSuffixForMetadata := '/metadata'
    let $doc := doc($pathInput || $tgURI || $pathInputSuffix)
    let $docForHeader := doc($pathInput || $tgURI || $pathInputSuffix)
    let $docForMetadata := doc($pathInputMetadata || $tgURI || $pathInputSuffix)
    let $pathOutput := '/db/apps/sade-architrave/templates/'|| $filenameOutput

    let $header := transformXML:magicForHeader($docForHeader//tei:TEI/*)
    let $metadata := local:magicForMetadata($docForMetadata/*)

mmarkus1's avatar
bugfix    
mmarkus1 committed
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
    let $input := $doc//tei:TEI/tei:text//tei:body
    let $counterPages := count($input//tei:pb)
    let $pbNumbers as xs:string := local:countPB($counterPages)
    let $fwNumbers as xs:string := local:countFW($counterPages,$input)

    for $i in 1 to $counterPages
            return
                if ($i < $counterPages) then (
                    let $first := $i
                    let $next := $i+1
                    let $output := local:milestone-chunk-ns($input//tei:pb[@n=string($first)],$input//tei:pb[@n=string($next)],$input)
                    let $outputXML := code-view:main($output)
                    let $output := local:magic($output)
                    let $filenameOutputPage := $filenameOutput || '_p' || string($i)
                    let $output := <div>{$metadata}<div id="tei-header"><span style="display:none" id="pbNumbers">{$pbNumbers}</span><span style="display:none" id="fwNumbers">{$fwNumbers}</span>{$header}</div>{$output}</div>
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '.html' , $output )
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '_xml.html' , $outputXML )
                    return $output
                )
                else (
                    let $output := local:milestone-chunk-ns-last($input//tei:pb[@n=string($counterPages)], ($input//tei:pb[@n="1"]/following-sibling::*[last()]),$input)
                    let $outputXML := code-view:main($output)
                    let $output := local:magic($output)
                    let $filenameOutputPage := $filenameOutput || '_p' || string($i)
                    let $output := <div>{$metadata}<div id="tei-header"><span style="display:none" id="pbNumbers">{$pbNumbers}</span><span style="display:none" id="fwNumbers">{$fwNumbers}</span>{$header}</div>{$output}</div>
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '.html' , $output )
                    let $store := xmldb:store($pathOutput, $filenameOutputPage || '_xml.html' , $outputXML )
                    return $output
                )
226
};
mmarkus1's avatar
mmarkus1 committed
227

mmarkus1's avatar
bugfix    
mmarkus1 committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
declare function transformXML:storeInPages($filenameInput as xs:string, $filenameOutput as xs:string) {
    let $login := xmldb:login("/db", "admin", "" )
    let $pathInput := '/db/apps/sade-architrave/textgrid/data'
    let $pathOutput := '/db/apps/sade-architrave/templates/'||$filenameInput
    let $doc := doc($pathInput||'/'||$filenameInput||'.xml')

    let $doc3 := doc($pathInput||'/'||$filenameInput||'.xml')
    let $header := transformXML:magicForHeader($doc3//tei:TEI/*)

    let $pathInput := '/db/apps/sade-architrave/textgrid/meta'
    let $doc2 := doc($pathInput||'/'||$filenameInput||'.xml')
    let $metadata := local:magicForMetadata($doc2/*)

    let $input := $doc//tei:TEI/tei:text//tei:body
    let $counterPages := count($input//tei:pb)
    let $pbNumbers := ''
    let $fwNumbers := ''
245

mmarkus1's avatar
bugfix    
mmarkus1 committed
246
247
    let $pbNumbers := local:countPB($counterPages)
    let $fwNumbers := local:countFW($counterPages,$input)
248

mmarkus1's avatar
bugfix    
mmarkus1 committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    for $i in 1 to $counterPages
        return
            if ($i < $counterPages) then (
                let $first := $i
                let $next := $i+1
                let $output := local:milestone-chunk-ns($input//tei:pb[@n=string($first)],$input//tei:pb[@n=string($next)],$input)
                let $outputXML := code-view:main($output)
                let $output := local:magic($output)
                let $filenameOutputPage := $filenameOutput || '_p' || string($i)
                let $output := <div>{$metadata}<div id="tei-header"><span style="display:none" id="pbNumbers">{$pbNumbers}</span><span style="display:none" id="fwNumbers">{$fwNumbers}</span>{$header}</div>{$output}</div>
                let $store := xmldb:store($pathOutput, $filenameOutputPage || '.html' , $output )
                let $store := xmldb:store($pathOutput, $filenameOutputPage || '_xml.html' , $outputXML )
                return $output
            )
            else (
                let $output := local:milestone-chunk-ns-last($input//tei:pb[@n=string($counterPages)], ($input//tei:pb[@n="1"]/following-sibling::*[last()]),$input)
                let $outputXML := code-view:main($output)
                let $output := local:magic($output)
                let $filenameOutputPage := $filenameOutput || '_p' || string($i)
                let $output := <div>{$metadata}<div id="tei-header"><span style="display:none" id="pbNumbers">{$pbNumbers}</span><span style="display:none" id="fwNumbers">{$fwNumbers}</span>{$header}</div>{$output}</div>
                let $store := xmldb:store($pathOutput, $filenameOutputPage || '.html' , $output )
                let $store := xmldb:store($pathOutput, $filenameOutputPage || '_xml.html' , $outputXML )
                return $output
            )
};
mmarkus1's avatar
mmarkus1 committed
274

mmarkus1's avatar
bugfix    
mmarkus1 committed
275
276
277
278
279
280
281
declare function transformXML:store($filenameInput as xs:string, $filenameOutput as xs:string) {

    let $pathInput := '/db/apps/sade-architrave/textgrid/data'
    let $pathOutput := '/db/apps/sade-architrave/templates'
    let $doc := doc($pathInput||'/'||$filenameInput||'.xml')
    let $xml := local:magic($doc/*)

282
    let $numberOfPages := $doc//tei:pb
mmarkus1's avatar
bugfix    
mmarkus1 committed
283
284
285
286
287
288
289
290
291
292
293

    let $pathInput := '/db/apps/sade-architrave/textgrid/meta'
    let $doc2 := doc($pathInput||'/'||$filenameInput||'.xml')
    let $metadata := local:magicForMetadata($doc2/*)

    let $result := <div data-template="templates:surround" data-template-with="templates/edition_surround.html" data-template-at="edition-container">{$metadata}{$xml}</div>

    let $login := xmldb:login("/db", "admin", "" )
    let $store := xmldb:store($pathOutput, $filenameOutput || '.html' , $result )

    return $result
mmarkus1's avatar
mmarkus1 committed
294
295
};

mmarkus1's avatar
bugfix    
mmarkus1 committed
296
297
declare function local:magicForMetadata($nodes) {
    for $node in $nodes
mmarkus1's avatar
mmarkus1 committed
298
return
mmarkus1's avatar
bugfix    
mmarkus1 committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
typeswitch ( $node )
    case element( tg:MetadataContainerType ) return ( local:magicForMetadata($node/node()) )
    case element( tg:object ) return ( local:magicForMetadata($node/node()) )
    case element( tg:generic ) return ( local:magicForMetadata($node/node()) )
    case element( tg:generated ) return ( local:magicForMetadata($node/node()) )
    case element( tg:created ) return
        <span style="display:none" id="tei-meta-dateCreated">{ string($node) }</span>
    case element( tg:lastModified ) return
        <span style="display:none" id="tei-meta-dateModified">{ string($node) }</span>
    case element( tg:textgridUri ) return
        <span style="display:none" id="tei-meta-textGridURI">{ string($node) }</span>
    case element( tg:pid ) return
        <span style="display:none" id="tei-meta-pid">{ string($node) }</span>
    default return ()
mmarkus1's avatar
mmarkus1 committed
313
314
};

mmarkus1's avatar
bugfix    
mmarkus1 committed
315
declare function transformXML:toHTML($file as xs:string) {
mmarkus1's avatar
mmarkus1 committed
316

mmarkus1's avatar
bugfix    
mmarkus1 committed
317
318
319
320
    let $doc := doc($file)
    let $result := local:magic($doc/*)

    return $result
mmarkus1's avatar
mmarkus1 committed
321
};
322
323
324
325
326
327
328
declare function transformXML:MetadataToHTML($file as xs:string) {

    let $doc := doc($file)
    let $result := local:magicForMetadata($doc/*)

    return $result
};
mmarkus1's avatar
mmarkus1 committed
329

mmarkus1's avatar
bugfix    
mmarkus1 committed
330
331
332
333
334
335
336
declare function transformXML:magicForHeader($nodes) {
for $node in $nodes
return
typeswitch ( $node )
    case element( tei:teiHeader ) return ( transformXML:magicForHeader($node/node()) )
    case element( tei:fileDesc ) return ( transformXML:magicForHeader($node/node()) )
    case element( tei:titleStmt ) return ( transformXML:magicForHeader($node/node()) )
337
338
339
340
    case element( tei:sourceDesc ) return ( transformXML:magicForHeader($node/node()) )
    case element( tei:msDesc ) return ( transformXML:magicForHeader($node/node()) )
    case element( tei:msContents ) return ( transformXML:magicForHeader($node/node()) )
    case element( tei:msItem ) return ( transformXML:magicForHeader($node/node()) )
mmarkus1's avatar
bugfix    
mmarkus1 committed
341
    case element( tei:title ) return
342
        if (string($node/@type) = '') then (
343
344
        <h4 id="tei-title-main-{string($node/@xml:lang)}" style="display:none" class="maintitle lang-{string($node/@xml:lang)} type-{string($node/@type)}" type="{string($node/@type)}" xml:lang="{string($node/@xml:lang)}">{ string($node) }</h4> )
        else (
mmarkus1's avatar
bugfix    
mmarkus1 committed
345
        <h4 id="tei-title-{string($node/@type)}-{string($node/@xml:lang)}" style="display:none" class="maintitle lang-{string($node/@xml:lang)} type-{string($node/@type)}" type="{string($node/@type)}" xml:lang="{string($node/@xml:lang)}">{ string($node) }</h4>
346
        )
mmarkus1's avatar
bugfix    
mmarkus1 committed
347
348
349
350
    case element( tei:author ) return
        <span style="display:none" id="tei-author">{ string($node) }</span>
    default return ()
};
351

mmarkus1's avatar
bugfix    
mmarkus1 committed
352
353
354
declare function local:magic($nodes) {
for $node in $nodes
return
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    try 
    {
        typeswitch ( $node )
            case element( tei:TEI ) return
                <div class="{$node/local-name()}">
                    { local:magic($node/node()) }
                </div>
            case element( tei:text ) return
                <div class="{$node/local-name()}">
                    { local:magic($node/node()) }
                </div>
            case element( tei:body ) return
                <div class="{$node/local-name()}">
                    { local:magic($node/node()) }
                </div>
            case element ( tei:div ) return
                <div class="{$node/local-name()}" type="{string($node/@type)}">
                    { local:magic($node/node()) }
                </div>
            case element ( tei:p ) return
                <p>{ local:magic($node/node()) }</p>
            case element ( tei:ab ) return
                <p>{ local:magic($node/node()) }</p>
            case element ( tei:pb ) return
                <span id="page{string($node/@n)}" class="pb" facs="{ string($node/@facs) }">{ string($node/@n) }</span>
            case element ( tei:fw ) return
                if (string($node/@place) = 'top-right') then (
382
                    <div class="tei-fw" style="text-align:left"><span class="fw {string($node/@place)}">{ local:magic($node/node()) }</span></div>
383
384
                )
                else (
385
                    <div class="tei-fw" style="text-align:left"><span class="fw {string($node/@place)}">{ local:magic($node/node()) }</span></div>
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
                )
            case element ( tei:choice ) return
                if (string('') = string('')) then (
                    <abbr class="abbr-yes" style="display:inline" title="{$node/tei:expan}">{ local:magic($node/tei:abbr) }</abbr>,
                    <abbr class="abbr-no" style="display:none" title="{$node/tei:abbr}">{ local:magic($node/tei:expan) }</abbr>
                )
                else ()
            case element (tei:date) return
                <span class="tei-date" data-toggle="tooltip" data-placement="bottom" title="{local:castDate(string($node/@when))}">{local:magic($node/node())}<i class="far fa-calendar-alt"></i></span>(: put a span around! is this ok?  :)
            case element ( tei:lb ) return
                <br/>
            case element ( tei:hi ) return
                <span class="hi {string($node/@rend)}" rend="{string($node/@rend)}">{ local:magic($node/node()) }</span>
            case element ( tei:supplied ) return
                <span class="tei-add"><span class="tei-add-bracket">⟨</span>{string($node)}<span class="tei-add-bracket">⟩</span></span>
            case element ( tei:placeName ) return
                if (($node/@subtype) = 'rooms') then (
                    <span class="placeName">{local:magic($node/node())}</span>,
                    <a class="placeName" href="#" onclick="openPlaceName('{substring-before(string($node/@ref),' ')}')" textgridURI="{substring-before(string($node/@ref),' ')}">
                        <i class="fas fa-map-marker-alt"></i>
                    </a>,
                    <a class="placeName" href="#" onclick="openPlaceName('{substring-after(string($node/@ref),' ')}')" textgridURI="{substring-after(string($node/@ref),' ')}">
                        <i class="fas fa-map-marker-alt"></i>
                    </a>
                )
                else (
                    <a class="placeName" href="#" onclick="openPlaceName('{string($node/@ref)}')" textgridURI="{$node/@ref}">
                        <span class="placeName">{local:magic($node/node())}</span>
                        <i class="fas fa-map-marker-alt"></i>
                    </a>
                )
            case element ( tei:geogName ) return
                <a class="placeName" href="#" onclick="openPlaceName('{string($node/@ref)}')" textgridURI="{$node/@ref}">
                    <span class="placeName">{local:magic($node/node())}</span>
                    <i class="fas fa-map-marker-alt"></i>
                </a>
            case element ( tei:persName ) return
                if (($node/@subtype) = 'persons') then (
                    <span class="persName">{local:magic($node/node())}</span>,
                    <a class="persName" href="#" onclick="openPersName('{substring-before(string($node/@ref),' ')}')" textgridURI="{substring-before(string($node/@ref),' ')}">
                        <i class="fas fa-user"></i>
                    </a>,
                    <a class="persName" href="#" onclick="openPersName('{substring-after(string($node/@ref),' ')}')" textgridURI="{substring-after(string($node/@ref),' ')}">
                        <i class="fas fa-user"></i>
                    </a>
                )
                else if (($node/@subtype) = 'family') then (
                    <a class="persName" href="#" onclick="openPersName('{string($node/@ref)}')" textgridURI="{$node/@ref}">
                        <span class="persName">{local:magic($node/node())}</span>
                        <i class="fas fa-users"></i>
                    </a>
                )
                else (
                    <a class="persName" href="#" onclick="openPersName('{string($node/@ref)}')" textgridURI="{$node/@ref}">
                        <span class="persName">{local:magic($node/node())}</span>
                        <i class="fas fa-user"></i>
                    </a>
                )
            case element ( tei:rs ) return
                <a class="ArtWork" href="#" onclick="openArtWork('{string($node/@ref)}')" textgridURI="{$node/@ref}" type="{string($node/@type)}">
                    <span class="ArtWork">{local:magic($node/node())}</span>
                    <i class="far fa-gem"></i>
                </a>
            case element ( tei:del ) return
                <span class="del"><strike>{string($node)}</strike></span>
            case element ( tei:unclear ) return
                <span class="unclear">{string($node)}[?]</span>
            case element ( tei:space ) return
                if (string($node/@extent) = '1word') then ( <span style="margin-right:1.25em;display:inline-block" class="tei-1word"></span> )
                else if (string($node/@extent) = '2word') then ( <span style="margin-right:2.5em;display:inline-block" class="tei-1word"></span> )
                else ()
            case element ( tei:num ) return
                if (string($node/@type) = 'fraction') then (
                    if (string($node/@rend) = 'horizontal') then (
                        <span class="tei-fraction-horizontal">
                            <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
                            <mrow>
                                <mfrac>
                                <mrow>
                                    <mn>{fn:substring-before(string($node/node()),'/')}</mn>
                                </mrow>
                                <mrow>
                                    <mn>{fn:substring-after(string($node/node()),'/')}</mn>
                                </mrow>
                                </mfrac>
                            </mrow>
                            </math>
                        </span>
                    )
                    else if (string($node/@rend) = 'oblique') then (
                        local:magic($node/node())
                    )
                    else if (string($node/@rend) = 'diagonal') then (
                        <span class="tei-fraction-oblique">
                            <sup>{fn:substring-before(string($node/node()),'/')}</sup>
                            /
                            <sub>{fn:substring-after(string($node/node()),'/')}</sub>
                        </span>
                    )
                    else ()
                )
                else if ((string($node/@type) = 'fractionLike') and (string($node/@rend) = 'horizontal')) then (
                    <span class="abbr-yes tei-fraction-horizontal" style="display:inline">
                        <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
                        <mrow>
                            <mfrac>
                            <mrow>
                                <mn>{fn:substring-before($node/text(),'/')}</mn>
                            </mrow>
                            <mrow>
                                <mn>{fn:substring-after($node/text(),'/')}</mn>
                            </mrow>
                            </mfrac>
                        </mrow>
                        </math>
                    </span>,
                    <span class="abbr-no tei-fraction-horizontal" style="display:none">
                        <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
                        <mrow>
                            <mfrac>
                            <mrow>
                                <mn>{string($node/tei:choice/tei:expan/node())}</mn>
                            </mrow>
                            <mrow>
                                <mn>{fn:substring-after($node/text(),'/')}</mn>
                            </mrow>
                            </mfrac>
                        </mrow>
                        </math>
                    </span>
                )
                else ( local:magic($node/node()) )
            case element ( tei:q ) return
                <span class="tei-quote">{string($node)}</span>
            case element ( tei:add ) return
                if ($node/@type = "asterisk") then (
                    if ($node/@place = "overleaf") then (
                        <span class="tei-add asterisk">{local:magic($node/node())}<span class="tei-add-tooltip" data-toggle="tooltip" data-placement="bottom" title="siehe nächste Seite"><i class="fas fa-star"></i></span></span>
                    )
                    else (
                        <span class="tei-add asterisk">{local:magic($node/node())}<span class="tei-add-tooltip" data-toggle="tooltip" data-placement="bottom" title="siehe Anmerkung"><i class="fas fa-star"></i></span></span>
                    )
                )
                else (
                    <span class="tei-add">{local:magic($node/node())}</span>
                )
            case element ( tei:teiHeader ) return (transformXML:magicForHeader($node))
            case text() return
                if( matches($node, "^\s+$") and not($node/parent::tei:p) ) then ()
                else if (matches($node, "&#11840;")) then (
                    <span>{fn:substring-before($node,"&#11840;")}</span>,
                    <span class="double-hyphen">&#61;</span>,
                    <span>{fn:substring-after($node,"&#11840;")}</span>
                )
                else
                <span class="txt">{$node}</span>
            case element ( tei:note ) return

                if ( string($node/@type) = "translation") then (
                    <a class="translationComment" href="#" onclick="openTranslationComment('{replace(replace(normalize-space(string-join($node//text(),'')),'&#34;',''),"&apos;","")}')">
                        <i class="fas fa-comment"/>
                    </a>
                )
                else if ( string($node/@type) = "scientific") then (
                    <a class="scientificComment" href="#" onclick="openScientificComment('{replace(replace(normalize-space(string-join($node//text(),'')),'&#34;',''),"&apos;","")}')">
                        <i class="fas fa-comment"/>
                    </a>
                )
                else if ( string($node/@type) = "editorial") then (
                    <a class="editorialComment" href="#" onclick="openEditorialComment('{replace(replace(normalize-space(string-join($node//text(),'')),'&#34;',''),"&apos;","")}')">
                        <i class="far fa-comment"/>
                    </a>
                )
                else (local:magic($node/node()))

561
562
563
            case element ( tei:table ) return 
                local:parse-table($node)

564
565
566
567
568
569
570
571
572
            default return
                local:magic($node/node())}
    
    catch * {
        element xhtml:span {
            attribute class {"error"},
            ($err:description)
        }
    }
mmarkus1's avatar
bugfix    
mmarkus1 committed
573
};
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

declare function local:parse-table($table as element(tei:table)) as element(xhtml:table) {
    let $head := $table/tei:head
    let $rows := $table/tei:row
    let $content := for $row in $rows 
                    let $cells := $row/tei:cell
                    return
                        element tr {
                            for $cell in $cells return
                                element td {local:magic($cell/node())}
                        }
    let $foot := $table/tei:trailer

    return
        element table {
            if ($head != "") then element thead {$head} else (),
            element tbody {
                $content
            },
            if ($foot != "") then element tfoot {$foot} else ()
        }
};