I have an XML file with a bunch of <entry>
elements in it (see below). I would like to extract most of the informations given in the <entry>
container and put them in a (X)HTML document.
I'm able to perform a search and get the wanted element contents. If I search for the term ἄγγελος
either in entry/hyperlemma/orth
(path A) or in cit/hyperlemma/orth
(path B), it is found once in entry01 in path A and twice in entry02 in path B.
The idea is that I print the content of each entry
container where ἄγγελος
was found, regardless of the amount of occurrences. As the term was found in entry02 twice, the entry gets (of course) printed twice, but I only need it once. Would that be possible to do with XQuery? And if so, how would I do that?
My XML:
<text>
<entry xml:id="01">
<hyperlemma>ἄγγελος</hyperlemma>
<lemma>ἄγγελος</lemma>
<variant>τῶν ἀγγέλων
<hyperlemma>
<orth>ἄγγελος</orth>
</hyperlemma>
</variant>
</entry>
<entry xml:id="02">
<hyperlemma>
<orth>ангелъ</orth>
</hyperlemma>
<lemma>
<orth>ангелъ</orth>
</lemma>
<variant>
<orth>анг꙯ла</orth>
<hyperlemma>
<orth>ангелъ</orth>
</hyperlemma>
<cit>
<hyperlemma>
<orth>ἄγγελος</orth>
</hyperlemma>
<lemma>
<orth>ἄγγελον</orth>
</lemma>
</cit>
</variant>
<variant>
<orth>анг꙯лъ</orth>
<hyperlemma>
<orth>ангелъ</orth>
</hyperlemma>
<cit>
<hyperlemma>
<orth>ἄγγελος</orth>
</hyperlemma>
<lemma>
<orth>ἄγγελος</orth>
</lemma>
</cit>
</variant>
</entry>
</text>
My XQuery:
xquery version "3.0";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "xml";
declare variable $searchphrase := "ἄγγελος";
<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Output of searchterm</h1>
<p>You are looking for "<font color="red"><strong>{$searchphrase}</strong></font>"</p>
{
let $hyperlemmas := doc("sample_entry.xml")/(descendant::entry | descendant::cit)/hyperlemma/orth [contains(., $searchphrase)]
return
<p>{$searchphrase} was found {count($hyperlemmas)} times.</p>
}
{
let $hyperlemmas := doc("sample_entry.xml")/(descendant::entry | descendant::cit)/hyperlemma/orth [contains(., $searchphrase)]
for $hyperlemma in $hyperlemmas
let $entry_id := $hyperlemma/ancestor::entry/@xml:id
let $lemma := $hyperlemma/ancestor::entry/lemma/orth
let $variant := $hyperlemma/ancestor::entry/variant/orth
return
<div>
Entry {string($entry_id)}:<br/>
Lemma: {$lemma} //
{
for $form in $variant
return
<i>{$form}</i>
}
</div>
}
</body>
</html>