Search code examples
xqueryexist-dbtei

eXist-db Lucene / KWIC output - linking to URL for document that produced a $hit


In eXist-DB 4.4 I have managed to deploy a simple Lucern query with KWIC output as a table.

I have a collection of tei:xml documents which looks like this sample:

<TEI xml:id="MS609-0001.xml">
 <text xml:id="MS609-0001">
   [...]
    <seg type="dep_event" subtype="event" xml:id="MS609-0001-1">
           <pb n="1r"/>
           <lb break="n" n="1"/>
           <date type="deposition_date" when="1245-05-27" cert="high">Anno
              Domini M° CC° XL° quinto VI Kalendas Iunii.</date>  
           <persName nymRef="#Arnald_Garnier_MSP-AU" role="dep">Arnaldus Garnerii</persName> 
           testis iuratus dixit quod vidit in 
           <placeName type="event_loc" nymRef="#home_of_Cap-de-Porc">domo 
              <persName nymRef="#Peire_Cap-de-Porc_MSP-AU" role="own">Petri de Sancto Andrea</persName>
           </placeName>
           <lb break="y" n="2"/>
           <persName nymRef="#Bernard_Cap-de-Porc_MSP-AU" role="her">B<supplied reason="expname">ernardum</supplied> de Sancto Andrea</persName>, 
           fratrem dicti Petri, et socium eius, hereticos. Et vidit ibi cum eis dictum
           <persName nymRef="#Peire_Cap-de-Porc_MSP-AU" ana="#uAdo" role="par">P<supplied reason="expname">etrum</supplied> de Sancto Andrea</persName> et 
           <persName nymRef="#Susanna_Cap-de-Porc_MSP-AU" ana="#uAdo" role="par">uxor dicti<lb break="y" n="3"/>Petri</persName>. Et 
           <persName nymRef="#Arnald_Garnier_MSP-AU" ana="#pAdo" role="par"/>ipse
           testis adoravit ibi dictos hereticos, sed non vidit alios adorare. Et 
           <date type="event_date" when="1239">sunt VI anni vel circa</date>. 
           <seg type="inq_int" subtype="specific_question">Et quando ipse testis exivit<lb break="y" n="4"/>domum invenit
                 <persName nymRef="#Guilhem_de_Rosengue_MSP-AU" key="inqint" ana="#pIntra" role="ref">Willelmus de Rozergue</persName> intrantem ad dictos hereticos.</seg>
        </seg>
        <seg>
          [...]
        </seg>
    [...]
  <text>
<TEI>

With this function calling KWIC:

 xquery version "3.1";

 declare namespace tei="http://www.tei-c.org/ns/1.0";
 import module namespace kwic="http://exist-db.org/xquery/kwic";

 let $query := 
   <query>
     <wildcard>heret*</wildcard>
   </query>

 for $hit in collection('/db/apps/deheresi/data/')//tei:seg[ft:query(.,$query)]
 order by ft:score($hit) descending
 return
    kwic:summarize($hit, <config width="80" table="yes" />)

I get for example these results as a table:

<tr>
   <td class="previous">...ernardum de Sancto Andrea, 
           fratrem dicti Petri, et socium eius, </td>
   <td class="hi">hereticos</td>
   <td class="following">. Et vidit ibi cum eis dictum
           Petrum de Sancto Andrea et 
   ...</td>
</tr>
<tr>
   <td class="previous">...r dicti Petri. Et ipse
           testis adoravit ibi dictos </td>
   <td class="hi">hereticos</td>
   <td class="following">, sed non vidit alios adorare. Et 
           sunt VI anni vel circa...</td>
</tr>

What I'd like to do is wrap the text in <td class="hi"/> in a url that points to the source document viewable on the site. The site logic is quite 'clean', such that the first entry's <td class="hi"> would look like this:

 <td class="hi"><a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0001">hereticos</a></td>

Where the url is a concat of

http://localhost:8081/exist/apps/deheresi/doc/ 

and the value of the respective result's ancestor node

 tei:text/@xml:id

(which will always be an ancestor node of whatever tei:seg content is returned in the query).

I note there is a @link attribute available on the <config> parameter in kwic:summarize(), but I don't know how to dynamically get the source document nodes from the returned result in order to fill that in.

Many thanks in advance.


Solution

  • It turns out that the node $hit allows access to the rest of the source document (or a copy of it in memory). Thus, I was able to build a URL as string using $hit/ancestor

    let $doclink :=  concat("http://localhost:8081/exist/apps/deheresi/doc/", $hit/ancestor::tei:text/data(@xml:id))                                                    
    

    Then fed that string into the function parameter @link:

     kwic:summarize($hit, <config width="80" table="yes" link="{$doclink}"/>)