-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.xq
138 lines (132 loc) · 7.17 KB
/
label.xq
1
2
3
4
5
6
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
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
132
133
134
135
136
137
138
xquery version "3.1";
import module namespace cc = "http://history.state.gov/ns/xquery/apps/consular-cards" at "consular-cards.xqm";
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare option output:method "html";
declare option output:html-version "5";
declare option output:media-type "text/html";
let $all-cards := collection("/db/apps/consular-cards/data")
let $label := request:get-parameter("label", ())[normalize-space(.) ne ""]
(: strip out angle brackets to avoid XSS :)
! replace(., "[<>]", "")
let $cards := $all-cards//tei:string[. eq $label]/ancestor::tei:surfaceGrp
return
(: Catch missing or invalid `label` parameter, temporary workaround to avoid XSS :)
if (empty($label) or empty($cards)) then
let $title := "Content Not Found"
let $content :=
<div>
<h2>Content Not Found</h2>
<p>No cards with the requested label were found. Please return to the homepage and try your search again.</p>
</div>
return
(
response:set-status-code(404),
cc:wrap-html($title, $content)
)
else
let $title := "Cards labeled “" || $label || "”"
let $start-years := $cards//tei:f[@name eq "year"]/tei:string[. ne ""]
let $content :=
<div>
<h2>{$title}</h2>
<p>{count($cards)} card{"s"[count($cards) gt 1]} whose start years {
let $min := min($start-years)
let $max := max($start-years)
return
if ($min eq $max) then
"begin with " || $min
else
"range from " || $min || " to " || $max}.</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Cards</th>
<th>Faces</th>
</tr>
</thead>
<tbody>
{
for $card in $cards
let $card-id := root($card)/tei:TEI/@xml:id/string()
let $faces := $card/tei:surface
let $prev-card-id := root($card)//tei:relatedItem[@type eq "prev"]/@target[. ne ""]
let $prev-card-label := $all-cards/tei:TEI[@xml:id eq $prev-card-id]//tei:title/string()
let $next-card-id := root($card)//tei:relatedItem[@type eq "next"]/@target[. ne ""]
let $next-card-label := $all-cards/tei:TEI[@xml:id eq $next-card-id]//tei:title/string()
return
<tr>
<td>
<dt>Card ID</dt>
<dd><a href="card.xq?id={$card-id}">{$card-id}</a></dd>
<dt>Number of faces</dt>
<dd>{count($faces)}</dd>
<dt>Navigation</dt>
{
if ($prev-card-id) then
<dd><a href="card.xq?id={$prev-card-id}">Previous card</a> ({$prev-card-label})</dd>
else
(),
if ($next-card-id) then
<dd><a href="card.xq?id={$next-card-id}">Next card</a> ({$next-card-label})</dd>
else
()
}
</td>
<td>
<table class="table">
<thead>
<tr>
<th>Face</th>
<th>Image</th>
<th>Transcription</th>
</tr>
</thead>
<tbody>{
for $face in $faces
let $face-id := $face/@xml:id/string()
let $sequence-no := $face//tei:f[@name eq "scan-sequence"]/tei:string/string()
let $label := $face//tei:f[@name eq "label"]/tei:string/string()
let $color := $face//tei:f[@name eq "color"]/tei:symbol/@value/string()
let $status := $face//tei:f[@name eq "status"]/tei:symbol/@value/string()
let $order := $face//tei:f[@name eq "order"]/tei:numeric/@value/string()
let $year := $face//tei:f[@name eq "year"]/tei:string/string()
let $src :=
"https://static.history.state.gov/consular-cards/color/medium/"
|| $face/tei:graphic/@url
=> replace("\.tif", ".jpg")
(:
"/exist/apps/consular-cards-new/images/" || replace($face/tei:graphic/@url, "\.tif", ".jpg")
:)
let $transcription := cc:tei-to-html(root($card)//tei:div[@corresp eq "#" || $face-id]/node())
return
<tr>
<td>
<dt>Face ID</dt>
<dd><a href="face.xq?id={$face-id}">{$face-id}</a></dd>
<dt>Sequence No.</dt>
<dd>{$sequence-no}</dd>
<dt>Label</dt>
<dd>{$label}</dd>
<dt>Color</dt>
<dd>{$color}</dd>
<dt>Status</dt>
<dd>{$status}</dd>
<dt>Order</dt>
<dd>{$order}</dd>
<dt>Start Year</dt>
<dd>{$year}</dd>
</td>
<td><img src="{$src}"/></td>
<td style="border: 1px solid black">{$transcription}</td>
</tr>
}</tbody>
</table>
</td>
</tr>
}
</tbody>
</table>
</div>
return
cc:wrap-html($title, $content)