I have a collection of two XML files (tagged according to TEI standard and containing lots of <entry>
elements) in an eXist database. I'm looping through the file with a FLWOR routine, looking for the search term the user typed in a form ($searchterm
) and returning the contents of certain elements. So far so good. I now would like to add another thing, but I don't know how to do that:
I would like to count how many <entry>
had $searchterm
in <form type="hyperlemma">
and print it instead of xxx
. If $searchterm
equals "ангелъ", the number would be "3". As I need to print that information before I loop through the collection, I have no clue what I need to do.
Could anyone help? As I'm still new to XQuery, my code probably isn't very pretty, so any hints that help me to improve it are much appreciated, too!
My XQuery:
xquery version "3.0";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare option exist:serialize "method=xhtml media-type=text/html";
declare variable $searchphrase := request:get-parameter("searchphrase", ());
declare variable $collection_path := "/db/apps/ex02/data";
<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8"/>
<title>{$page-title}</title>
</head>
<body>
<h1>{$page-title}</h1>
<h2>Results</h2>
<p>Your input: "{$searchphrase}"</p>
<h3>Found xxx entries</h3>
{
for $file in collection($collection_path),
$hyperlemma in $file/(descendant::tei:entry | descendant::tei:cit)/tei:form[@type='hyperlemma']/tei:orth [ft:query(., $searchphrase)]
let $title := $hyperlemma/ancestor::*/tei:head
let $entry_number := $hyperlemma/ancestor::tei:entry[1]/@xml:id
let $lemma := $hyperlemma/ancestor::tei:entry/tei:form[@type='lemma']/tei:orth
return
<div>
{string($title)} ({data($entry_number)}):<br/>
<strong><font color="red">{string($lemma)}</font></strong><br/>
{
for $counterpart in $hyperlemma/ancestor::tei:entry/(tei:form[@type='lemma'] | tei:form[@type='variant'])/tei:cit/tei:form[@type='lemma']/tei:orth
return
<font color="green">    {string($counterpart)}<br/></font>
}
</div>
}
</body>
These are snippets of the two XML files:
(A)
...
<text>
<head>Euch.</head>
<entry xml:id="pen-26">
<form type="hyperlemma" xml:lang="grc">
<orth>ἄγγελος</orth>
</form>
<form type="lemma" xml:lang="grc">
<orth>ἄγγελος</orth>
<cit type="counterpart" xml:lang="cu">
<form type="hyperlemma" xml:lang="cu">
<orth>ангелъ</orth>
</form>
<form type="lemma" xml:lang="cu">
<orth>аньꙉелъ</orth>
</form>
</cit>
</form>
</entry>
<entry xml:id="pen-336">
<form type="hyperlemma" xml:lang="grc">
<orth>ἀρχάγγελος</orth>
</form>
<form type="lemma" xml:lang="grc">
<orth>ἀρχάγγελος</orth>
<cit type="counterpart" xml:lang="cu">
<form type="hyperlemma" xml:lang="cu">
<orth>ангелъ</orth>
</form>
<form type="lemma" xml:lang="cu">
<orth>аньꙉелъ</orth>
</form>
</cit>
</form>
</entry>
</text>
...
(B)
...
<text>
<head>Syn.Tr. [1]</head>
<entry xml:id="tas-12">
<form type="hyperlemma" xml:lang="grc">
<orth>ἄγγελος</orth>
</form>
<form type="lemma" xml:lang="grc">
<orth>ἄγγελος</orth>
<cit type="counterpart" xml:lang="cu">
<form type="hyperlemma" xml:lang="cu">
<orth>ангелъ</orth>
</form>
<form type="lemma" xml:lang="cu">
<orth>ангєлъ</orth>
</form>
</cit>
<cit type="counterpart" xml:lang="cu">
<form type="hyperlemma" xml:lang="cu">
<orth>вѣстьникъ</orth>
</form>
<form type="lemma" xml:lang="cu">
<orth>вѣстьникъ</orth>
</form>
</cit>
</form>
</entry>
</text>