Skip to content

Commit 37dc142

Browse files
committed
[refactor] Get the labels before transforming to Markdown. This enables us to in-future replace the transformation to Markdown (that is currently written as an XQuery typeswitch transformation) with an XSLT transformation
1 parent de7cbff commit 37dc142

1 file changed

Lines changed: 75 additions & 21 deletions

File tree

src/main/xar-resources/services/get-rec.xql

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ declare variable $local:exist-collection-path as xs:string := rh:request-param("
3535

3636

3737
(: Recurse through child nodes :)
38-
declare function local:markdown($nodes as node()*) as item()* {
38+
declare function local:markdown($nodes as node()*, $labels as element(labels)?) as item()* {
3939
for $node in $nodes
4040
return
4141
typeswitch($node)
@@ -57,13 +57,13 @@ declare function local:markdown($nodes as node()*) as item()* {
5757
if ($node[parent::tei:quote] or $node[parent::tei:summary] or $node[parent::tei:note] or $node[parent::tei:desc] or $node[parent::tei:ab]) then
5858
if ($node[following-sibling::tei:p]) then
5959
(
60-
local:markdown($node/node()),
60+
local:markdown($node/node(), $labels),
6161
"

"
6262
)
6363
else
64-
local:markdown($node/node())
64+
local:markdown($node/node(), $labels)
6565
else
66-
local:passthru($node)
66+
local:passthru($node, $labels)
6767

6868
case element(tei:lb)
6969
return
@@ -73,7 +73,7 @@ declare function local:markdown($nodes as node()*) as item()* {
7373
return
7474
(
7575
"*",
76-
local:markdown($node/node()),
76+
local:markdown($node/node(), $labels),
7777
"*"
7878
)
7979

@@ -83,50 +83,103 @@ declare function local:markdown($nodes as node()*) as item()* {
8383
element {node-name($node)} {
8484
$node/@*[not(local-name(.) = ('active', 'mutual', 'passive'))],
8585
for $ref in tokenize($node/@active, " ")[. ne ""]
86-
let $label := local:get-label($ref)
86+
let $label := $labels/label[@ref eq $ref]/text/text()
8787
return
8888
<tei:active ref="{$ref}">{$label}</tei:active>
8989
,
9090
for $ref in tokenize($node/@mutual, " ")[. ne ""]
91-
let $label := local:get-label($ref)
91+
let $label := $labels/label[@ref eq $ref]/text/text()
9292
return
9393
<tei:mutual ref="{$ref}">{$label}</tei:mutual>
9494
,
9595
for $ref in tokenize($node/@passive, " ")[. ne ""]
96-
let $label := local:get-label($ref)
96+
let $label := $labels/label[@ref eq $ref]/text/text()
9797
return
9898
<tei:passive ref="{$ref}">{$label}</tei:passive>
9999
,
100-
local:markdown($node/node())
100+
local:markdown($node/node(), $labels)
101101
}
102102

103103
else
104-
local:passthru($node)
104+
local:passthru($node, $labels)
105105

106106
case element()
107107
return
108-
local:passthru($node)
108+
local:passthru($node, $labels)
109109

110110
default
111111
return
112-
local:markdown($node/node())
112+
local:markdown($node/node(), $labels)
113113
};
114114

115115
(: Recurse through child nodes :)
116-
declare function local:passthru($node as node()*) as item()* {
116+
declare function local:passthru($node as node()*, $labels as element(labels)?) as item()* {
117117
element {node-name($node)} {
118118
$node/@*,
119-
local:markdown($node/node())
119+
local:markdown($node/node(), $labels)
120120
}
121121
};
122122

123-
declare function local:get-label($ref as xs:string) as xs:string {
124-
let $collection := "/db/apps/majlis-data/data"
125-
let $expanded-refs := ($ref, concat($ref, "/"), concat($ref, "/tei"))
126-
let $doc := head(collection($collection)//tei:idno[@type eq "URI"][. = $expanded-refs]/ancestor::tei:TEI)
127-
let $label := $doc/descendant::tei:title[1]/descendant-or-self::text()
123+
(:~
124+
: Given the a node from a TEI document, we first extract the refs from all descendant tei:relation active, mutual, or passive attributes.
125+
: Secondly, for each ref we try and find a TEI document in the database whose tei:idno matches the ref,
126+
: if we find a document, we use it to generate a label for the ref.
127+
:
128+
: @param $node the TEI node to start resolving tei:relation from
129+
:
130+
: @return A labels element containing refs and their labels, e.g. <labels><label ref="https://jalit.org/person/34">Bahīya</label></labels>, or if there are no labels, then an empty sequence.
131+
:)
132+
declare function local:get-labels($node as node()?) as element(labels)? {
133+
134+
(: Get the relation refs from the the source TEI document - these are our lookup keys for later finding the labels :)
135+
let $refs as xs:string* :=
136+
distinct-values(
137+
for $relation in $node//tei:relation[exists((@active, @mutual, @passive))]
138+
let $active := tokenize($relation/@active[string-length(normalize-space(.)) gt 0]/string(), " ")
139+
let $mutual := tokenize($relation/@mutual[string-length(normalize-space(.)) gt 0]/string(), " ")
140+
let $passive := tokenize($relation/@passive[string-length(normalize-space(.)) gt 0]/string(), " ")
141+
return
142+
($active, $mutual, $passive)[. ne ""]
143+
)
128144
return
129-
normalize-space($label)
145+
146+
(: Now we try and find a label for each one of those refs in the database :)
147+
let $labels :=
148+
let $expanded-refs := $refs ! (., concat(., "/"), concat(., "/tei")) (: Expand each ref to 3 possible refs :)
149+
let $collection := "/db/apps/majlis-data/data"
150+
let $id-nos := collection($collection)//tei:idno[@type eq "URI"][. = $expanded-refs]
151+
return
152+
153+
(: Iterate through the expanded-refs in sets of 3 :)
154+
for $i in 1 to xs:integer(count($expanded-refs) div 3)
155+
let $set-of-expanded-refs := ($expanded-refs[$i * 3 - 2], $expanded-refs[$i * 3 - 1], $expanded-refs[$i * 3])
156+
return
157+
158+
if ($id-nos[. = $set-of-expanded-refs[1]])
159+
then
160+
<label ref="{$set-of-expanded-refs[1]}">
161+
<text>{$id-nos[. = $set-of-expanded-refs[1]]/ancestor::tei:TEI[1]/descendant::tei:title[1]/descendant-or-self::text()}</text>
162+
</label>
163+
164+
else if ($id-nos[. = $set-of-expanded-refs[2]])
165+
then
166+
<label ref="{$set-of-expanded-refs[2]}">
167+
<text>{$id-nos[. = $set-of-expanded-refs[2]]/ancestor::tei:TEI[1]/descendant::tei:title[1]/descendant-or-self::text()}</text>
168+
</label>
169+
170+
else if ($id-nos[. = $set-of-expanded-refs[3]])
171+
then
172+
<label ref="{$set-of-expanded-refs[3]}">
173+
<text>{$id-nos[. = $set-of-expanded-refs[3]]/ancestor::tei:TEI[1]/descendant::tei:title[1]/descendant-or-self::text()}</text>
174+
</label>
175+
else
176+
() (: NOTE(AR) there is no label for this ref... that is okay as an @active, @mutual, or @passive attribute may contain multiple refs and some of those refs may be external to the database, and therefore we can't resolve them :)
177+
return
178+
if (exists($labels))
179+
then
180+
<labels>{$labels}</labels>
181+
else
182+
()
130183
};
131184

132185
declare function local:get-data() {
@@ -195,5 +248,6 @@ if ($local:github = "browse") then
195248
response:stream($json, "method=TEXT media-type=application/json")
196249
else
197250
let $data := local:get-data()
251+
let $labels := local:get-labels($data)
198252
return
199-
local:markdown($data)
253+
local:markdown($data, $labels)

0 commit comments

Comments
 (0)