view software/eXist/webapp/mpdl/presentation/pageHtml.xsl @ 15:e99964f390e4

diverse Fehlerbehebungen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Mon, 29 Aug 2011 17:40:19 +0200
parents 469d927b9ca7
children 7e883ce72fec
line wrap: on
line source

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:functx="http://www.functx.com"
  xmlns:saxon="http://saxon.sf.net/"
  xmlns:mpdl="http://www.mpiwg-berlin.mpg.de/ns/mpdl"
  xmlns:mpdl-util="http://www.mpiwg-berlin.mpg.de/ns/mpdl/util"
  xmlns:text="http://www.mpiwg-berlin.mpg.de/ns/mpdl/text"
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:dcterms="http://purl.org/dc/terms"
  xmlns:echo="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/" 
  xmlns:math="http://www.w3.org/1998/Math/MathML"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:import href="/db/mpdl/presentation/functions-mpdl.xsl" />
<xsl:import href="/db/mpdl/presentation/functions-text.xsl" />
<xsl:import href="/db/mpdl/presentation/functions-util.xsl" />

<xsl:strip-space elements="*"/> 

<xsl:output method="xhtml" encoding="utf-8"/>

<xsl:variable name="errorMessage" select="string(/result/query/result/error)"/>
<xsl:variable name="mode" select="/result/page/mode"/>
<xsl:variable name="queryType" select="/result/query/type"/>
<xsl:variable name="query" select="text:trim(string(/result/query/expression))"/>
<xsl:variable name="queryResultPages" select="number(/result/query/result/pages)"/>
<xsl:variable name="queryResultPN" select="/result/query/result/pn"/>
<xsl:variable name="language" select="/result/document-description/language"/>
<xsl:variable name="sn" select="number(/result/page/sentence-number)"/>
<xsl:variable name="highlightElement" select="/result/page/highlightElement"/>
<xsl:variable name="highlightElementPos" select="number(/result/page/highlightElementPos)"/>
<xsl:variable name="digilibAvailable" select="/result/page/digilib-available"/>
<xsl:variable name="options" select="/result/page/options"/>
<xsl:variable name="topPB" select="subsequence(//pb, 1, 1)"/>
<xsl:variable name="firstSentence" select="subsequence(//s, 1, 1)"/>
<xsl:variable name="ftQueryTermsTemp" select="string-join(text:translateLuceneToTerms($query), '')" as="xs:string"/>
<xsl:variable name="ftQueryMorphForms" select="string(/result/query/result/query-forms)"/>
<!-- Removes the duplicates between ftQueryMorphForms and ftQueryTerms for highlighting them semantically correct: -->
<!-- Morphological forms are hightlighted as whole words but query terms are highlighted also as partly strings -->
<xsl:variable name="ftQueryTerms" select="text:removeDuplicates($ftQueryTermsTemp, $ftQueryMorphForms)"/> 
<xsl:variable name="ftQueryRegularizations" select="string(/result/query/result/query-regularizations)"/>
<xsl:variable name="ftQueryHighlightWords">
  <xsl:choose>
  <xsl:when test="$ftQueryMorphForms != '' and $ftQueryRegularizations = ''"><xsl:value-of select="$ftQueryMorphForms"/></xsl:when>
  <xsl:when test="$ftQueryMorphForms = '' and $ftQueryRegularizations != ''"><xsl:value-of select="$ftQueryRegularizations"/></xsl:when>
  <xsl:when test="$ftQueryMorphForms != '' and $ftQueryRegularizations != ''"><xsl:value-of select="concat($ftQueryMorphForms, '|', $ftQueryRegularizations)"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$ftQueryMorphForms"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<!-- used in module: functions-text: highlight  -->
<xsl:variable name="ftQueryEncodeBig5" select="/result/query/result/big5-mappings"/>
<xsl:variable name="ftQueryMode">
  <xsl:choose>
  <xsl:when test="matches($query, concat('&quot;', '.*', '&quot;'))">
    <xsl:value-of select="'phrase'"/>
  </xsl:when>
  <xsl:when test="string-length($query) = 0">
    <xsl:value-of select="'false'"/>
  </xsl:when>
  <!-- Because there are  problems in recognizing words (in mode word) this fake mode is used (same as phrase mode)  -->
  <xsl:otherwise><xsl:value-of select="'fakeWord'"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:template match="result">
  <xsl:variable name="author" select="string-join(/result/document-description/authors/author, ', ')"/>
  <xsl:variable name="title" select="string-join(/result/document-description/titles/title, ', ')"/>
  <xsl:variable name="place" select="string-join(/result/document-description/places/place, ', ')"/>
  <xsl:variable name="date" select="/result/document-description/date"/>
  <xsl:variable name="bookTitle">
    <xsl:value-of select="$author"/>.
    <xsl:value-of select="$title"/>.
    <xsl:value-of select="$place"/><xsl:if test="$place != ''">, </xsl:if>
    <xsl:value-of select="$date"/>.
  </xsl:variable>
  <html>
  <head>
    <title><xsl:value-of select="$bookTitle"/></title>
    <link rel="stylesheet" type="text/css" href="presentation/pageHtml.css"/>
  </head>
  <body>
    <text style="font-weight:bold;font-size:20px"><xsl:value-of select="$bookTitle"/></text>
    <p/>
    <xsl:for-each select="page">
      <xsl:variable name="documentUri" select="/result/document-description/uri"/>
      <xsl:variable name="documentName" select="/result/document-description/document-name"/>
      <xsl:variable name="countPages" select="/result/document-description/count-pages"/>
      <xsl:variable name="pageHeader" select="header"/>
      <xsl:variable name="pageNumber" select="number(number)"/>
      <xsl:variable name="pageNumberOrig" select="number-orig"/>
      <xsl:variable name="documentValue" select="concat('document=', $documentUri)"/>
      <xsl:variable name="pnValue" select="concat('pn=', $pageNumber)"/>
      <xsl:variable name="modeValue" select="concat('mode=', $mode)"/>
      <table cellspacing="1" cellpadding="1">
        <colgroup>
          <col width="5%"/>
          <col width="5%"/>
          <col width="5%"/>
          <col width="5%"/>
          <col width="33%"/>
          <col width="3%"/>
          <col width="3%"/>
          <col width="3%"/>
          <col width="3%"/>
          <col width="3%"/>
          <col width="3%"/>
          <col width="3%"/>
          <col width="10%"/>          
          <col width="3%"/>
          <col width="5%"/>
          <col width="3%"/>
          <col width="5%"/>
        </colgroup>
        <tr>
        <form action="page-query-result.xql" method="get">
        <input type="hidden" name="document" value="{$documentUri}"/>
        <td>
          <xsl:choose>
          <xsl:when test="$mode = 'text'"><button id="buttonModeText" name="mode" value="text" style="background:none;border:none;"><img src="images/text.jpg" height="18"/></button></xsl:when>
          <xsl:otherwise><button id="buttonModeUText" name="mode" value="text" style="background:none;border:none;"><img src="images/textU.jpg" height="18"/></button></xsl:otherwise>
          </xsl:choose>
        </td>
        <td>
          <xsl:choose>
          <xsl:when test="$mode = 'textPollux'"><button id="buttonModeTextPollux" name="mode" value="textPollux" style="background:none;border:none;"><img src="images/textPollux.jpg" height="18"/></button></xsl:when>
          <xsl:otherwise><button id="buttonModeUTextPollux" name="mode" value="textPollux" style="background:none;border:none;"><img src="images/textPolluxU.jpg" height="18"/></button></xsl:otherwise>
          </xsl:choose>
        </td>
        <td>
          <xsl:choose>
          <xsl:when test="$mode = 'image'"><button id="buttonModeImage" name="mode" value="image" style="background:none;border:none;"><img src="images/image.jpg" height="18"/></button></xsl:when>
          <xsl:otherwise><button id="buttonModeUImage" name="mode" value="image" style="background: none;border:none;"><img src="images/imageU.jpg" height="18"/></button></xsl:otherwise>
          </xsl:choose>
        </td>
        <td>
          <xsl:choose>
          <xsl:when test="$mode = 'xml'"><button id="buttonModeXml" name="mode" value="xml" style="background:none;border:none;"><img src="images/xml.jpg" height="18"/></button></xsl:when>
          <xsl:otherwise><button id="buttonModeUXml" name="mode" value="xml" style="background:none;border:none;"><img src="images/xmlU.jpg" height="18"/></button></xsl:otherwise>
          </xsl:choose>
        </td>
        <input type="hidden" name="pn" value="{$pageNumber}"/>
        <xsl:if test="/result/page/highlightElement[node()]">
          <input type="hidden" name="highlightElement" value="{$highlightElement}"/>
        </xsl:if>
        <xsl:if test="/result/page/highlightElementPos[node()]">
          <input type="hidden" name="highlightElementPos" value="{$highlightElementPos}"/>
        </xsl:if>
        <input type="hidden" name="query-type" value="{$queryType}"/>
        <xsl:if test="/result/query/result[node()]">
          <input type="hidden" name="query" value="{$query}"/>
          <input type="hidden" name="query-result-pn" value="{$queryResultPN}"/>
        </xsl:if>
        </form>
        <td/>
        <!-- toc, fulltext, fulltextMorph, structural, ftIndex and ftIndexMorph buttons      -->
        <form action="page-query-result.xql" method="get">
        <input type="hidden" name="document" value="{$documentUri}"/>
        <input type="hidden" name="mode" value="{$mode}"/>
        <input type="hidden" name="pn" value="{$pageNumber}"/>
        <td><button id="bToc" name="query-type" value="toc" style="background: none; border: none;"><img src="images/toc.gif" width="24" height="24"/></button></td>
        <td><button id="bFulltext" name="query-type" value="fulltext" style="background: none; border: none;"><img src="images/search.gif" width="24" height="24"/></button></td>
        <td><button id="bFulltextMorph" name="query-type" value="fulltextMorph" style="background: none; border: none;"><img src="images/searchMorph.gif" width="24" height="24"/></button></td>
        <td><button id="bStructural" name="query-type" value="xpath" style="background: none; border: none;"><img src="images/searchStructural.gif" width="24" height="24"/></button></td>
        <td><button id="bFtIndex" name="query-type" value="ftIndex" style="background: none; border: none;"><img src="images/dictionary.gif" width="24" height="24"/></button></td>
        <td><button id="bftIndexMorph" name="query-type" value="ftIndexMorph" style="background: none; border: none;"><img src="images/dictionaryMorph.gif" width="24" height="24"/></button></td>
        <td><button id="bFigures" name="query-type" value="figures" style="background: none; border: none;"><img src="images/figures.png" width="24" height="24"/></button></td>
        <xsl:choose>
          <xsl:when test="/result/query/result[node()] and not($queryType = 'toc')">
            <input type="hidden" name="query" value=""/>
            <input type="hidden" name="query-result-pn" value="{$queryResultPN}"/>
          </xsl:when>
          <xsl:when test="/result/query/result[node()] and $queryType = 'toc'">
            <input type="hidden" name="query-result-pn" value="1"/>
          </xsl:when>
          <xsl:otherwise>
          </xsl:otherwise>
        </xsl:choose>
        </form>
        <td/>
        <xsl:variable name="leftNumber">
          <xsl:choose>
          <xsl:when test="$pageNumber &gt; 1"><xsl:value-of select="$pageNumber - 1"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$pageNumber"/></xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="rightNumber">
          <xsl:choose>
          <xsl:when test="$pageNumber &lt; $countPages"><xsl:value-of select="$pageNumber + 1"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$pageNumber"/></xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <form action="page-query-result.xql" method="get">
        <input type="hidden" name="document" value="{$documentUri}"/>
        <input type="hidden" name="mode" value="{$mode}"/>
        <td><button id="buttonLeftNumber" name="pn" value="{$leftNumber}" style="background:none;border:none;"><img src="images/left.gif"/></button></td>
        <td nowrap="true"><xsl:value-of select="$pageNumber"/> / <xsl:value-of select="$countPages"/></td>
        <td><button id="buttonRightNumber" name="pn" value="{$rightNumber}" style="background:none;border:none;"><img src="images/right.gif"/></button></td>
        <input type="hidden" name="query-type" value="{$queryType}"/>
        <xsl:if test="/result/query/result[node()]">
          <input type="hidden" name="query" value="{$query}"/>
          <input type="hidden" name="query-result-pn" value="{$queryResultPN}"/>
        </xsl:if>
        </form>
        <form action="page-query-result.xql" method="get">
        <input type="hidden" name="document" value="{$documentUri}"/>
        <input type="hidden" name="mode" value="{$mode}"/>
        <td nowrap="true">Page: <input type="text" size="2" name="pn" value="{$pageNumber}"/></td> 
        <input type="hidden" name="query-type" value="{$queryType}"/>
        <xsl:if test="/result/query/result[node()]">
          <input type="hidden" name="query" value="{$query}"/>
          <input type="hidden" name="query-result-pn" value="{$queryResultPN}"/>
        </xsl:if>
        </form>
        </tr>
      </table>
      <hr/>
      <table align="middle" width="100%" rules="cols" frame="void" cellpadding="7">
        <colgroup>
          <col width="65%"/>
          <col width="35%"/>
        </colgroup>
        <tr>
          <xsl:for-each select="content">
            <xsl:choose>
            <xsl:when test="$mode = 'text' or $mode = 'textPollux' or $mode = 'gis'">
              <td align="left" valign="top">
                <xsl:if test="$pageHeader != '' or $pageNumberOrig != ''">
                  <table width="100%" cellspacing="1" cellpadding="1">
                    <colgroup>
                      <col width="10%"/>
                      <col width="70%"/>
                      <col width="20%"/>
                    </colgroup>
                    <tr>
                      <td align="left"><b><xsl:value-of select="$pageNumberOrig"/></b></td>
                      <td align="center"><b><xsl:value-of select="$pageHeader"/></b></td>
                    </tr>
                  </table>
                </xsl:if>
                <table>
                <colgroup>
                  <col width="90%"/>
                  <col width="10%"/>
                </colgroup>
                <tr>
                <td>
                  <xsl:variable name="contentStr" select="normalize-space(string(.))"/>
                  <xsl:variable name="figures" select=".//figure|.//handwritten"/>
                  <xsl:if test="$contentStr = '' and empty($figures)">
                    <div class="emptyPage"><xsl:value-of select="'[Empty page]'"/></div>
                  </xsl:if>
                  <xsl:apply-templates mode="text"/>
                </td>
                <td align="middle" valign="top"><p><a href="?{$documentValue}&amp;mode={$mode}&amp;pn={$pageNumber}&amp;sn={$sn}&amp;export=pdf&amp;options={$options}"><img src="images/download.png" width="24" height="24" border="0" alt="Download"/></a><br/>Download PDF</p></td>
                </tr>
                </table>
              </td>
            </xsl:when>
            <xsl:when test="$mode = 'xml'">
              <td align="left" valign="top">
                <table>
                <colgroup>
                  <col width="90%"/>
                  <col width="10%"/>
                </colgroup>
                <tr>
                <td><xsl:apply-templates mode="xml"/></td>
                <td align="middle" valign="top"><p><a target="_blank" href="?{$documentValue}&amp;mode=pureXml&amp;pn={$pageNumber}"><img src="images/download.png" width="24" height="24" border="0" alt="Download"/></a><br/>Download XML</p></td>
                <td align="middle" valign="top"><p><a href="?{$documentValue}&amp;mode={$mode}&amp;pn={$pageNumber}&amp;sn={$sn}&amp;export=pdf"><img src="images/download.png" width="24" height="24" border="0" alt="Download"/></a><br/>Download PDF</p></td>
                </tr>
                </table>
              </td>
            </xsl:when>
            <xsl:when test="$mode = 'image'">
              <xsl:variable name="imageAvailable" select="/result/page/image-available"/>
              <xsl:variable name="imageFileName" select="/result/page/image-file-name"/>
              <xsl:variable name="linkImageEcho" select="/result/page/image-echo"/>
              <xsl:variable name="linkImageScaler" select="/result/page/image-scaler"/>
              <xsl:variable name="imageHeight" select="600"/>
              <td align="middle" valign="top">
                <xsl:choose>
                  <xsl:when test="$digilibAvailable = 'true' and $imageAvailable = 'true'">
                    <table>
                      <colgroup>
                        <col width="90%"/>
                        <col width="10%"/>
                      </colgroup>
                      <tr>
                      <td>
                        <div style="height:{$imageHeight}px; margin-left:10px; margin-right:10px; border: 1px;">
                          <a href="{$linkImageEcho}"><img alt="Page image: {$linkImageScaler}" src="{$linkImageScaler}&amp;dh={$imageHeight}"/></a>
                        </div>
                      </td>
                      <td align="middle" valign="top"><p><a href="?{$documentValue}&amp;mode={$mode}&amp;pn={$pageNumber}&amp;sn={$sn}&amp;export=pdf"><img src="images/download.png" width="24" height="24" border="0" alt="Download"/></a><br/>Download PDF</p></td>
                      </tr>
                    </table>
                  </xsl:when>
                  <xsl:when test="$digilibAvailable = 'true' and $imageAvailable = 'false'">
                    <div style="height:{$imageHeight}px; margin-left:10px; margin-right:10px; border:1px dashed;">
                      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
                      Page image: <br/>
                      <br/>
                      <xsl:value-of select="$imageFileName"/> , page <xsl:value-of select="$pageNumber"/>
                      <br/><br/>
                      not scanned
                    </div>
                  </xsl:when>
                  <xsl:when test="$digilibAvailable = 'false'">
                    <div style="height:{$imageHeight}px; margin-left:10px; margin-right:10px; border:1px dashed;">
                      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
                      Could not fetch: <br/>
                      <br/>
                      <xsl:value-of select="$imageFileName"/> , page <xsl:value-of select="$pageNumber"/>
                      <br/><br/>
                      from nausikaa2.rz-berlin.mpg.de: please try again later
                    </div>
                  </xsl:when>
                  <xsl:otherwise></xsl:otherwise>
                </xsl:choose>
              </td>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
          <td align="left" valign="top" style="padding-right: 25px;">
            <xsl:if test="$queryType = 'toc'">
              <b>Table of contents</b>
            </xsl:if>
            <xsl:if test="$queryType = 'figures'">
              <b>Figures</b>
            </xsl:if>
            <xsl:if test="$queryType = 'fulltext' or $queryType = 'fulltextMorph' or $queryType = 'fulltextMorphLemma'">
            <form action="page-query-result.xql" method="get">
              <input type="hidden" name="document" value="{$documentUri}"/>
              <input type="hidden" name="pn" value="{$pageNumber}"/>
              <input type="hidden" name="mode" value="{$mode}"/>
              <input type="hidden" name="query-type" value="{$queryType}"/>
              <xsl:if test="$queryType = 'fulltext'">
                <xsl:value-of select="'Contains: '"/>
              </xsl:if>
              <xsl:if test="$queryType = 'fulltextMorph'">
                <xsl:value-of select="'Contains morphological: '"/>
              </xsl:if>
              <xsl:if test="$queryType = 'fulltextMorphLemma'">
                <xsl:value-of select="'Contains lemma: '"/>
              </xsl:if>
              <input type="text" size="15" name="query" value="{$query}"/>
              <input type="hidden" name="query-result-pn" value="1"/>
              <input type="submit" value="Query"/>
            </form>
            </xsl:if>
            <xsl:if test="$queryType = 'xpath' or $queryType = 'xquery'">
            <form action="page-query-result.xql" method="get">
              <input type="hidden" name="document" value="{$documentUri}"/>
              <input type="hidden" name="pn" value="{$pageNumber}"/>
              <input type="hidden" name="mode" value="{$mode}"/>
              <table cellspacing="1" cellpadding="1">
              <colgroup>
                <col width="70%"/>
                <col width="15%"/>
                <col width="15%"/>
              </colgroup>
              <tr>
                <td>
                  <xsl:if test="$queryType = 'xpath'"><b><xsl:value-of select="'XPath query '"/></b></xsl:if>
                  <xsl:if test="$queryType = 'xquery'"><b><xsl:value-of select="'XQuery '"/></b></xsl:if>
                  <a href="info.xql?info={$queryType}" onclick="window.open(&quot;info.xql?info={$queryType}&quot;, &quot;InfoWindow&quot;, &quot;menubar=no,width=500,height=500,toolbar=no&quot;);return false"><img src="images/info.png" width="15" height="15" border="0" alt="Info"/></a>
                </td>
                <td><button id="bXPath" name="query-type" value="xpath" style="background:none"><text style="font-weight:bold;font-size:12px">XPath</text></button></td>
                <td><button id="bXQuery" name="query-type" value="xquery" style="background:none"><text style="font-weight:bold;font-size:12px">XQuery</text></button></td>
              </tr>
              </table>
              <input type="hidden" name="query" value=""/>
            </form>
            <form action="page-query-result.xql" method="get">
              <input type="hidden" name="document" value="{$documentUri}"/>
              <input type="hidden" name="pn" value="{$pageNumber}"/>
              <input type="hidden" name="mode" value="{$mode}"/>
              <xsl:if test="$queryType = 'xpath'">
                <input type="hidden" name="query-type" value="xpath"/>
                <table>
                <tr><td><input type="text" size="50" name="query" value="{$query}"/></td></tr>
                <tr><td><input type="submit" value="Query"/></td></tr>
                </table>
              </xsl:if>
              <xsl:if test="$queryType = 'xquery'">
                <input type="hidden" name="query-type" value="xquery"/>
                <table>
                  <tr>
                    <td>
                    <textarea name="query" cols="45" rows="10">
                      <xsl:choose>
                      <xsl:when test="$query = ''"><xsl:value-of select="' '" /></xsl:when>
                      <xsl:otherwise><xsl:value-of select="$query"/></xsl:otherwise>
                      </xsl:choose>
                    </textarea>
                    </td>
                  </tr>
                  <tr><td><input type="submit" value="Query"/></td></tr>
                </table>
              </xsl:if>
            </form>
            </xsl:if>
            <xsl:if test="$queryType = 'ftIndex' or $queryType = 'ftIndexMorph'">
            <form action="page-query-result.xql" method="get">
              <input type="hidden" name="document" value="{$documentUri}"/>
              <input type="hidden" name="pn" value="{$pageNumber}"/>
              <input type="hidden" name="mode" value="{$mode}"/>
              <input type="hidden" name="query-type" value="{$queryType}"/>
              <xsl:if test="$queryType = 'ftIndex'"><xsl:value-of select="'Fulltext index: '"/></xsl:if>
              <xsl:if test="$queryType = 'ftIndexMorph'"><xsl:value-of select="'Morphological index: '"/></xsl:if>
              <input type="text" size="10" name="query" value="{$query}"/> 
              <input type="hidden" name="query-result-pn" value="1"/>
              <input type="submit" value="Query"/>
            </form>
            </xsl:if>
            <xsl:if test="(($query != '' or $queryType = 'toc' or $queryType = 'figures') and $queryResultPages &gt; 0)">
              <hr/>
              <table>
              <colgroup>
                <col width="81%"/>
                <col width="3%"/>
                <col width="5%"/>
                <col width="3%"/>
                <col width="5%"/>
              </colgroup>
              <tr>
              <td>
                <xsl:variable name="resultSize" select="/result/query/result/size"/>
                <xsl:if test="$queryType = 'fulltext' or $queryType = 'fulltextMorph' or $queryType = 'fulltextMorphLemma'">
                  <b><xsl:value-of select="$resultSize"/> hits</b>
                </xsl:if>
                <xsl:if test="$queryType = 'ftIndex' or $queryType = 'ftIndexMorph'">
                  <b><xsl:value-of select="$resultSize"/> entries beginning with: "<xsl:value-of select="$query"/>"<br/></b>
                </xsl:if>
                <xsl:if test="$queryType = 'xpath' or $queryType = 'xquery'">
                  <b><xsl:value-of select="$resultSize"/> element hits</b>
                </xsl:if>
              </td>
              <xsl:variable name="leftNumber">
                <xsl:choose>
                <xsl:when test="$queryResultPN &gt; 1"><xsl:value-of select="$queryResultPN - 1"/></xsl:when>
                <xsl:otherwise><xsl:value-of select="$queryResultPN"/></xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:variable name="rightNumber">
                <xsl:choose>
                <xsl:when test="$queryResultPN &lt; $queryResultPages"><xsl:value-of select="$queryResultPN + 1"/></xsl:when>
                <xsl:otherwise><xsl:value-of select="$queryResultPN"/></xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <form action="page-query-result.xql" method="get">
              <input type="hidden" name="document" value="{$documentUri}"/>
              <input type="hidden" name="pn" value="{$pageNumber}"/>
              <input type="hidden" name="mode" value="{$mode}"/>
              <input type="hidden" name="query-type" value="{$queryType}"/>
              <input type="hidden" name="query" value="{$query}"/>
              <td><button id="buttonLeftNumber" name="query-result-pn" value="{$leftNumber}" style="background:none;border:none;"><img src="images/left.gif"/></button></td>
              <td nowrap="true"><xsl:value-of select="$queryResultPN"/> / <xsl:value-of select="$queryResultPages"/></td>
              <td><button id="buttonRightNumber" name="query-result-pn" value="{$rightNumber}" style="background:none;border:none;"><img src="images/right.gif"/></button></td>
              </form>
              <form action="page-query-result.xql" method="get">
              <input type="hidden" name="document" value="{$documentUri}"/>
              <input type="hidden" name="pn" value="{$pageNumber}"/>
              <input type="hidden" name="mode" value="{$mode}"/>
              <input type="hidden" name="query-type" value="{$queryType}"/>
              <input type="hidden" name="query" value="{$query}"/>
              <td nowrap="true">Page: <input type="text" size="2" name="query-result-pn" value="{$queryResultPN}"/></td>
              </form>
              </tr>
              </table>
              <xsl:if test="$ftQueryMorphForms != ''">
                <table>
                  <colgroup>
                    <col width="40%"/>
                    <col width="60%"/>
                  </colgroup>
                  <td/>
                  <xsl:if test="$queryType = 'fulltextMorph'">
                    <td align="right" nowrap="true">Morphological expansion: <a href="lt/lemma.xql?language={$language}&amp;query={$query}">see here</a></td>
                  </xsl:if>
                  <xsl:if test="$queryType = 'fulltextMorphLemma'">
                    <td align="right" nowrap="true">Morphological expansion: <a href="lt/lemma.xql?language={$language}&amp;lemma={$query}">see here</a></td>
                  </xsl:if>
                </table>
              </xsl:if>
              <hr/>
              <xsl:if test="$queryType = 'toc' or $queryType = 'figures'">
                <table class="toc" cellspacing="1" cellpadding="1">
                  <colgroup>
                    <col width="70%"/>
                    <col width="20%"/>
                  </colgroup>
                <xsl:for-each select="/result/query/result/hits/toc-entry">
                  <xsl:variable name="page" select="page"/>
                  <xsl:variable name="level">
                    <xsl:choose>
                      <xsl:when test="real-level = '1'"><xsl:value-of select="'one'"/></xsl:when>
                      <xsl:when test="real-level = '2'"><xsl:value-of select="'two'"/></xsl:when>
                      <xsl:when test="real-level = '3'"><xsl:value-of select="'three'"/></xsl:when>
                      <xsl:when test="real-level = '4'"><xsl:value-of select="'four'"/></xsl:when>
                      <xsl:when test="real-level = '5'"><xsl:value-of select="'five'"/></xsl:when>
                      <xsl:when test="real-level = '6'"><xsl:value-of select="'six'"/></xsl:when>
                      <xsl:when test="real-level = '7'"><xsl:value-of select="'seven'"/></xsl:when>
                      <xsl:otherwise><xsl:value-of select="'one'"/></xsl:otherwise>
                    </xsl:choose>
                  </xsl:variable>
                  <tr>
                  <td align="left" valign="top">
                    <div class="toc {$level}">
                      <xsl:value-of select="'['"/>
                      <xsl:value-of select="concat(level-string, ' ')"/>
                      <xsl:value-of select="content"/>
                      <xsl:value-of select="']'"/>
                    </div>
                  </td>
                  <td align="center" valign="top">
                    <div class="toc float right">
                      <a href="?{$documentValue}&amp;pn={$page}&amp;{$modeValue}&amp;query-type={$queryType}&amp;query-result-pn={$queryResultPN}">
                        <xsl:value-of select="concat('Page: ', $page)"/>
                      </a>
                    </div>
                  </td>
                  </tr>
                </xsl:for-each>
                </table>
              </xsl:if>
              <xsl:if test="$queryType = 'fulltext' or $queryType = 'fulltextMorph' or $queryType = 'fulltextMorphLemma'">
              <table cellspacing="1" cellpadding="1">
              <xsl:for-each select="/result/query/result/hits/hit">
                <tr valign="top">
                <xsl:variable name="pos" select="pos"/>
                <xsl:variable name="term" select="term"/>
                <td><xsl:value-of select="$pos"/>.</td>
                <td align="left">
                  <xsl:variable name="hitType" select="hitType"/>
                  <xsl:variable name="hitPN" select="pn"/>
                  <xsl:variable name="hitString" select="hitString"/>
                  <xsl:variable name="hitPos" select="hitPos"/>
                  <xsl:variable name="hitSurroundsPB" select="hitSurroundsPB"/>
                  <xsl:variable name="queryValue" select="concat('&amp;', 'query=', $query)"/>
                  <xsl:variable name="queryValueEscaped1" select="replace($queryValue, '\+', '%2B')"/>
                  <xsl:variable name="queryValueEscaped2" select="replace($queryValueEscaped1, ' ', '+')"/>
                  <xsl:choose>
                  <!-- if a found sentence surrounds a page break then a special presentation of the hit is done -->
                  <xsl:when test="$hitType = 's' and $hitSurroundsPB = 'false'">
                    <a href="?{$documentValue}&amp;pn={$hitPN}&amp;{$modeValue}&amp;query-type={$queryType}{$queryValueEscaped2}&amp;query-result-pn={$queryResultPN}&amp;highlightElement={$hitType}&amp;highlightElementPos={$hitPos}#s{$hitPos}">Page <xsl:value-of select="$hitPN"/>, Sentence <xsl:value-of select="$hitPos"/></a>
                  </xsl:when>
                  <xsl:when test="$hitType = 'head'">
                    <a href="?{$documentValue}&amp;pn={$hitPN}&amp;{$modeValue}&amp;query-type={$queryType}{$queryValueEscaped2}&amp;query-result-pn={$queryResultPN}&amp;highlightElement={$hitType}&amp;highlightElementPos={$hitPos}#head{$hitPos}">Page <xsl:value-of select="$hitPN"/>, Head <xsl:value-of select="$hitPos"/></a>
                  </xsl:when>
                  <xsl:otherwise>
                    <a href="?{$documentValue}&amp;pn={$hitPN}&amp;{$modeValue}&amp;query-type={$queryType}{$queryValueEscaped2}&amp;query-result-pn={$queryResultPN}&amp;highlightElement={$hitType}&amp;highlightElementPos={$hitPos}#s{$hitPos}">Page <xsl:value-of select="$hitPN"/>, Sentence <xsl:value-of select="$hitPos"/></a> / <a href="?{$documentValue}&amp;pn={$hitPN + 1}&amp;{$modeValue}&amp;query-type={$queryType}{$queryValueEscaped2}&amp;query-result-pn={$queryResultPN}#s0">Page <xsl:value-of select="$hitPN + 1"/>, continuation of the sentence</a>
                  </xsl:otherwise>
                  </xsl:choose>
                  :<br/>
                  <!-- Highlight the query terms in each hit sentence and clip the result -->
                  <xsl:sequence select="text:highlight($hitString, $ftQueryTerms, $ftQueryHighlightWords, 'true')"/>
                </td>
                </tr>
              </xsl:for-each>
              </table>
              </xsl:if>
              <xsl:if test="$queryType = 'ftIndex' or $queryType = 'ftIndexMorph'">
              <table cellspacing="1" cellpadding="1">
              <xsl:for-each select="/result/query/result/hits/entry">
                <tr valign="top">
                <xsl:variable name="pos" select="position"/>
                <xsl:variable name="term" select="term"/>
                <xsl:variable name="polluxKeys" select="pollux-keys"/>
                <xsl:variable name="lemmasWithOR" select="lemmas-with-or"/>
                <xsl:variable name="dictionaryLink">
                  <xsl:choose>
                  <xsl:when test="$language = 'zh'">
                    <xsl:variable name="termBig5Encoded" select="/result/query/result/big5-hits/entry-big5-encoded/term[$pos]"/>
                    <a href="http://humanum.arts.cuhk.edu.hk/cgi-bin/agrep-lindict?query={$termBig5Encoded}&amp;category=wholerecord">Lin Yutang</a>
                    <!-- TODO: replace Java based function by XSL-function like the following, but that does not work yet cause termHexBin is not the same than by Java) also in query.xsl and functions-text.xsl -->
                    <xsl:variable name="termHexBin"><xsl:value-of select="saxon:string-to-hexBinary($term, 'utf8')"/></xsl:variable>
                    <xsl:variable name="termHexBinStr" select="string($termHexBin)"/>
                    <xsl:variable name="termBig5EncodedNew">
                      <xsl:for-each select="string-to-codepoints($termHexBinStr)">
                        <xsl:variable name="pos" select="position()"/>
                        <xsl:variable name="posIDiv" select="$pos idiv 2"/>
                        <xsl:variable name="posPlus1IDiv" select="($pos + 1) idiv 2"/>
                        <xsl:if test="$posIDiv != $posPlus1IDiv">
                          <xsl:variable name="charStr" select="substring($termHexBinStr, $pos, 2)"/>
                          <xsl:value-of select="encode-for-uri(concat('%', $charStr))"/>
                        </xsl:if>
                      </xsl:for-each>
                    </xsl:variable>
                  </xsl:when>
                  <xsl:otherwise><a href="http://archimedes.mpiwg-berlin.mpg.de/cgi-bin/toc/dict?step=table;word={$term};lang={$language};pro=echo">Echo</a></xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:variable name="polluxLink">
                  <xsl:choose>
                  <xsl:when test="$polluxKeys = ''">Pollux</xsl:when>
                  <xsl:otherwise><a href="lt/lex.xql?language={$language}&amp;query={$polluxKeys}">Pollux</a></xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:variable name="wikipediaLink">
                  <xsl:choose>
                  <xsl:when test="$lemmasWithOR = ''">Wiki</xsl:when>
                  <xsl:otherwise><a href="http://{$language}.wikipedia.org/wiki/index.php?search={$lemmasWithOR}">Wiki</a></xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:variable name="ftSearchQueryType">
                  <xsl:choose>
                  <xsl:when test="$queryType = 'ftIndex'"><xsl:value-of select="'fulltext'"/></xsl:when>
                  <xsl:when test="$queryType = 'ftIndexMorph'"><xsl:value-of select="'fulltextMorphLemma'"/></xsl:when>
                  <xsl:otherwise></xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <td><xsl:value-of select="$pos"/>.</td>
                <td align="left">
                  <a href="lt/lemma.xql?language={$language}&amp;lemma={$term}"><xsl:value-of select="$term"/></a>(<a href="?{$documentValue}&amp;{$modeValue}&amp;query-type={$ftSearchQueryType}&amp;query={$term}"><xsl:value-of select="frequency"/></a>) [<xsl:sequence select="$polluxLink"/>][<xsl:sequence select="$wikipediaLink"/>][<xsl:sequence select="$dictionaryLink"/>]
                </td>
                </tr>
              </xsl:for-each>
              </table>
              </xsl:if>
              <xsl:if test="$queryType = 'xpath' or $queryType = 'xquery'">
                <xsl:for-each select="/result/query/result/hits">
                  <table>
                  <colgroup>
                    <col width="90%"/>
                    <col width="10%"/>
                  </colgroup>
                  <tr>
                  <td><xsl:apply-templates mode="xml"/></td>
                  <xsl:variable name="queryWithoutCRLF" select="replace($query, '\r\n', ' ')"/>
                  <td align="middle" valign="top"><p><a target="_blank" href="?{$documentValue}&amp;mode=pureXml&amp;query-type={$queryType}&amp;query={$queryWithoutCRLF}&amp;query-result-pn={$queryResultPN}"><img src="images/download.png" width="24" height="24" border="0" alt="Download"/></a><br/>Download</p></td>
                  </tr>
                  </table>
                </xsl:for-each>
              </xsl:if>
            </xsl:if>
            <xsl:choose>
              <xsl:when test="$errorMessage != ''">
                <hr/>
                <b>Your query delivers an error: </b>
                <xsl:value-of select="$errorMessage"/>
              </xsl:when>
              <xsl:when test="$errorMessage = '' and $query != '' and $queryResultPages = 0">
                <hr/>
                <b>No results</b>
              </xsl:when>
            </xsl:choose>
          </td>
        </tr>
      </table>
      <hr/>
      Elapsed time: <xsl:value-of select="/result/performance"/> ms, Back to <a href="query.xql">query page</a>, see the <a href="page-query-result.xql?_source=yes">XQuery source</a> and <a href="/exist/rest/db/mpdl/presentation/pageHtml.xsl?_source=yes">XSL source</a> of this page
      <br/>
    </xsl:for-each>
  </body></html>
</xsl:template>

<xsl:template match="attribute()|element()|text()|comment()|processing-instruction()" mode="xml">
  <xsl:copy>
    <xsl:apply-templates select="attribute()|element()|text()|comment()|processing-instruction()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="element()|comment()|processing-instruction()" mode="xml">
  <xsl:variable name="elementName" select="name()"/>
  <xsl:variable name="countPrecedingElemsStr" select="concat('count(preceding::', $elementName, '[. >> $p1]) + 1')"/>
  <xsl:variable name="elementPresentation">
    <xsl:choose>
    <xsl:when test="element() = node() or text() != '' or self::comment() or self::processing-instruction()">
      <xsl:value-of select="'&lt;'"/>
      <span class="xml elementName"><xsl:value-of select="$elementName"/></span>
      <xsl:apply-templates select="attribute()" mode="xml"/>
      <xsl:value-of select="'&gt;'"/>
      <xsl:apply-templates select="element()|text()|comment()|processing-instruction()" mode="xml"/>
      <xsl:value-of select="'&lt;/'"/>
      <span class="xml elementName"><xsl:value-of select="$elementName"/></span>
      <xsl:value-of select="'&gt;'"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="'&lt;'"/>
      <span class="xml elementName"><xsl:value-of select="$elementName"/></span>
      <xsl:apply-templates select="attribute()" mode="xml"/>
      <xsl:value-of select="'/&gt;'"/>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="currentHighlightElementPos">
    <xsl:choose>
      <xsl:when test="($highlightElement = 's' or $highlightElement = '') and . = $firstSentence and ($topPB >> .)">0</xsl:when>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:value-of select="saxon:evaluate($countPrecedingElemsStr, $topPB)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="saxon:evaluate($countPrecedingElemsStr, $topPB)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="class">
    <xsl:choose>
      <xsl:when test="($sn >= 0 and $sn = $currentHighlightElementPos) or ($elementName = $highlightElement and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos)"><xsl:value-of select="'xml element highlight'"/></xsl:when>
      <xsl:when test="$elementName = $highlightElement and $highlightElementPos >= 0 and $highlightElementPos != $currentHighlightElementPos"><xsl:value-of select="'xml element'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'xml element'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <!-- Show the sentence in color light grey if it is given as highlightElementPos -->
  <ul class="{$class}" id="{$elementName}{$currentHighlightElementPos}">
    <xsl:sequence select="$elementPresentation"/>
  </ul>
</xsl:template>

<xsl:template match="attribute()" mode="xml">
  <xsl:variable name="attributeName" select="name()"/>
  <span class="xml attributeName">
    <xsl:value-of select="' '"/>
    <xsl:value-of select="$attributeName"/>
  </span>
  <xsl:value-of select="'=&quot;'"/>
  <span class="xml attributeValue"><xsl:value-of select="."/></span><xsl:value-of select="'&quot;'"/>
  <xsl:apply-templates select="attribute()" mode="xml"/>
</xsl:template>

<!-- If ft-query is set then highlight all term occurrences in each little text piece for the fulltext query -->
<xsl:template match="text()" mode="xml">
  <xsl:variable name="ancestorStr" select="concat('./ancestor::', $highlightElement)"/>
  <xsl:variable name="highlightElementAncestor">
    <xsl:choose>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:sequence select="./ancestor::s"/></xsl:when>
      <xsl:otherwise><xsl:sequence select="saxon:evaluate($ancestorStr)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="highlightElementAncestorType" select="name($highlightElementAncestor/*[1])"/>
  <xsl:variable name="countPrecedingElemsStr" select="concat('count(preceding::', $highlightElement, '[. >> $p1]) + 1')"/>
  <xsl:variable name="currentHighlightElementPos">
    <xsl:choose>
      <xsl:when test="($highlightElement = 's' or $highlightElement = '') and $highlightElementAncestor = $firstSentence and ($topPB >> $highlightElementAncestor)">0</xsl:when>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:value-of select="saxon:evaluate($countPrecedingElemsStr, $topPB)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="saxon:evaluate($countPrecedingElemsStr, $topPB)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$ftQueryMode != 'false' and $highlightElementAncestorType = $highlightElement and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos">
      <xsl:sequence select="text:highlight(string(.), $ftQueryTerms, $ftQueryHighlightWords, 'false')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- variables used by templates in mode "text" -->
<xsl:variable name="collectionName" select="/result/document-description/collection-name"/>
<xsl:variable name="documentUri" select="/result/document-description/uri"/>
<xsl:variable name="documentName" select="/result/document-description/document-name"/>
<xsl:variable name="documentIdentifier" select="/result/document-description/identifier"/>
<xsl:variable name="pageNumber" select="/result/page/number"/>
<xsl:variable name="firstFigurePosition" select="/result/page/firstFigurePosition"/>
<xsl:variable name="figuresImageDirectory" select="/result/page/figures-image-directory"/>
<xsl:variable name="figures" select="/result/page/figures"/>
<xsl:variable name="handwritten" select="/result/page/handwritten"/>
<xsl:variable name="tables" select="/result/page/tables"/>
<xsl:variable name="notes" select="/result/page/notes"/>
<xsl:variable name="charNorm" select="/result/page/character-normalization"/>

<xsl:template match="text" mode="text">
  <xsl:apply-templates mode="text"/>
  <!--   TEI Dictionary                      -->
  <xsl:if test="not(empty(//entry))">
    <xsl:for-each select="//entry">
      <xsl:sort select="form/orth"/>
      <xsl:variable name="position" select="position()"/>
      <span class="entry">
        <xsl:apply-templates mode="text" select="form"/>
        <xsl:apply-templates mode="text" select="sense"/>
        <xsl:if test="not(empty(figure))">
          <span class="entryDiv">
            <span class="bf"><xsl:value-of select="'Figures: '"/></span>
            <span class="entryDiv">
              <xsl:for-each select="figure">
                <xsl:variable name="href" select="graphic/@url"/>
                <xsl:variable name="head" select="head"/>
                <xsl:variable name="figDesc" select="string(figDesc)"/>
                <xsl:if test="$href != ''">
                  <div class="figure" style="margin-left:10px;">
                    <a href="{$href}"><img alt="Figure: {$figDesc}" src="{$href}" width="200" height="200"/></a>
                    <br/>
                    <xsl:value-of select="'[Figure]: '"/><xsl:value-of select="$head"/>
                  </div>
                </xsl:if>
              </xsl:for-each>
            </span>
          </span>
        </xsl:if>
        <xsl:if test="not(empty(xr))">
          <span class="entryDiv">
            <span class="bf"><xsl:value-of select="'References: '"/></span>
            <span class="entryDiv">
              <xsl:for-each select="xr/ref">
                <li><xsl:apply-templates mode="text" select="."/></li>
              </xsl:for-each>
            </span>
          </span>
        </xsl:if>
      </span>
    </xsl:for-each>  
  </xsl:if>
  <!--   Notes                      -->
  <xsl:variable name="countEmptyPlaces" select="count($notes/note[empty(@place)])"/>
  <xsl:variable name="countBottomPlaces" select="count($notes/note[contains(@place, 'bottom')])"/>
  <xsl:if test="($collectionName = 'archimedes' or $collectionName = 'tei') and count($notes/*) > 0">
    <div>
      <xsl:choose>
        <xsl:when test="$countBottomPlaces > 0 or $countEmptyPlaces > 0">
          <hr class="notesBottom"/>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
      </xsl:choose>
      <xsl:for-each select="$notes/note">
        <xsl:variable name="notePos" select="position()"/>
        <xsl:variable name="place" select="@place"/>
        <xsl:variable name="label" select="$notePos"/>
        <xsl:variable name="uid" select="@uid"/>
        <xsl:variable name="modificationDate" select="@modificationDate"/>
        <xsl:variable name="noteWithoutNamespace" select="mpdl-util:copyWithoutNamespace(.)"/>
        <xsl:choose>
          <xsl:when test="$place = '' or empty($place) or $place = 'bottom'">
            <p>
              <a>
                <xsl:attribute name="name"><xsl:value-of select="concat('note-', $pageNumber, '-', $label)"/></xsl:attribute>
                <xsl:attribute name="href"><xsl:value-of select="concat(urlBase, '#', 'note-', $pageNumber, '-', $label, 'ref')"/></xsl:attribute>
                <xsl:attribute name="class"><xsl:value-of select="'note'"/></xsl:attribute>
                <xsl:value-of select="concat('note-', $pageNumber, '-', $label)"/>
              </a>
              <xsl:value-of select="': '"/>
              <xsl:choose>
                <xsl:when test="$uid = '' or empty($uid)">
                  <span class="note"><xsl:apply-templates select="$noteWithoutNamespace/node()" mode="text"/></span>
                </xsl:when>
                <xsl:otherwise>
                  <span class="note"><xsl:apply-templates select="$noteWithoutNamespace/node()" mode="text"/></span>
                  <xsl:value-of select="concat(' [external note, ', $uid, ', ', $modificationDate, ']')"/>
                </xsl:otherwise>
              </xsl:choose>
            </p>
          </xsl:when>
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </div>
  </xsl:if>
  <xsl:if test="$collectionName = 'echo' and count($notes/*) > 0">
    <div>
      <hr class="notesBottom"/>
      <xsl:for-each select="$notes/echo:note">
        <xsl:variable name="label" select="string(@xlink:label)"/>
        <xsl:variable name="uid" select="@uid"/>
        <xsl:variable name="modificationDate" select="@modificationDate"/>
        <xsl:variable name="noteWithoutNamespace" select="mpdl-util:copyWithoutNamespace(.)"/>
        <p>
          <a>
            <xsl:attribute name="name"><xsl:value-of select="$label"/></xsl:attribute>
            <xsl:attribute name="href"><xsl:value-of select="concat(urlBase, '#', $label, 'ref')"/></xsl:attribute>
            <xsl:attribute name="class"><xsl:value-of select="'note'"/></xsl:attribute>
            <xsl:value-of select="$label"/>
          </a>
          <xsl:value-of select="': '"/>
          <xsl:choose>
            <xsl:when test="$uid = '' or empty($uid)">
              <span class="note"><xsl:apply-templates select="$noteWithoutNamespace/node()" mode="text"/></span>
            </xsl:when>
            <xsl:otherwise>
              <span class="note"><xsl:apply-templates select="$noteWithoutNamespace/node()" mode="text"/></span>
              <xsl:value-of select="concat(' [external note, ', $uid, ', ', $modificationDate, ']')"/>
            </xsl:otherwise>
          </xsl:choose>
        </p>
      </xsl:for-each>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="head" mode="text">
  <xsl:variable name="currentHighlightElementPos">
    <xsl:choose>
      <xsl:when test="$highlightElement = 'head'"><xsl:value-of select="count(preceding::head[. >> $topPB]) + 1"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="0"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="class">
    <xsl:choose>
      <xsl:when test="not(empty(@style)) and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos"><xsl:value-of select="concat('head highlight bf ', @style)"/></xsl:when>
      <xsl:when test="empty(@style) and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos"><xsl:value-of select="'head highlight bf'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'head bf'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <p>
    <span>
      <xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="concat('head', $currentHighlightElementPos)"/></xsl:attribute>
      <xsl:apply-templates mode="text"/>
    </span>
  </p>
</xsl:template>

<!-- TEI: segmentation   -->
<xsl:template match="seg" mode="text">
  <xsl:choose>
    <xsl:when test="@rend = 'highlight'">
      <span class="seg highlight"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:when test="@rend = 'highlightPoint'">
      <span class="seg highlightPoint"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:otherwise>
      <span class="seg"><xsl:apply-templates mode="text"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- TEI: choice   -->
<xsl:template match="choice" mode="text">
  <xsl:if test="not(empty(orig))">
    <xsl:apply-templates select="orig" mode="text"/>
  </xsl:if>
  <xsl:if test="not(empty(abbr))">
    <xsl:apply-templates select="abbr" mode="text"/>
  </xsl:if>
  <xsl:if test="not(empty(am))">
    <xsl:apply-templates select="am" mode="text"/>
  </xsl:if>
  <xsl:if test="not(empty(sic))">
    <xsl:apply-templates select="sic" mode="text"/>
  </xsl:if>
</xsl:template>

<!-- TEI: hi (highlighted)  -->
<xsl:template match="hi" mode="text">
  <xsl:choose>
    <xsl:when test="@rend = 'initial'">
      <span class="dc-unmodified"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:when test="@rend = 'bold'">
      <span class="bf"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:otherwise>
      <span>
        <xsl:attribute name="class"><xsl:value-of select="@rend"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- TEI: name (of type: place, person, ...)   -->
<xsl:template match="name" mode="text">
  <xsl:choose>
    <xsl:when test="@type = 'place'">
      <span class="place" title="Place"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:when test="@type = 'person'">
      <span class="person" title="Person"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:when test="@type = 'org'">
      <span class="organization" title="Organization"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:otherwise>
      <span>
        <xsl:attribute name="class"><xsl:value-of select="@type"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- TEI: place, person, ...)   -->
<xsl:template match="placeName" mode="text">
  <span class="place" title="Place: {@type}"><xsl:apply-templates mode="text"/></span>
</xsl:template>
<xsl:template match="persName" mode="text">
  <span class="person" title="Person"><xsl:apply-templates mode="text"/></span>
</xsl:template>

<!-- TEI: term    -->
<xsl:template match="term" mode="text">
  <span class="term" title="Terminology entry"><xsl:apply-templates mode="text"/></span>
</xsl:template>

<!-- TEI: line    -->
<xsl:template match="lg" mode="text">
  <div class="lg"><xsl:apply-templates mode="text"/></div>
</xsl:template>

<xsl:template match="l" mode="text">
  <span class="l"><xsl:apply-templates mode="text"/></span><br/>
</xsl:template>

<!-- TEI: reference    -->
<xsl:template match="ref" mode="text">
  <span class="ref">
    <xsl:if test="not(empty(@target))">
      <a>
        <xsl:attribute name="href"><xsl:value-of select="@target"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </a>
    </xsl:if>
    <xsl:if test="empty(@target)">
      <xsl:apply-templates mode="text"/>
    </xsl:if>
  </span>
</xsl:template>

<!-- TEI: table    -->
<xsl:template match="table" mode="text">
  <table>
    <xsl:if test="not(empty(head))">
      <caption align="top"><xsl:apply-templates mode="text" select="head"/></caption>
    </xsl:if>
    <xsl:apply-templates mode="text" select="row"/>
  </table>
</xsl:template>

<xsl:template match="row" mode="text">
  <xsl:choose>
    <xsl:when test="@role = 'label'">
      <tr style="font-weight:bold;"><xsl:apply-templates mode="text"/></tr>
    </xsl:when>
    <xsl:when test="@role = 'data' or empty(@role)">
      <tr><xsl:apply-templates mode="text"/></tr>
    </xsl:when>
    <xsl:otherwise>
      <tr><xsl:apply-templates mode="text"/></tr>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="cell" mode="text">
  <xsl:choose>
    <xsl:when test="@role = 'label' and empty(@cols)">
      <td style="font-weight:bold;"><xsl:apply-templates mode="text"/></td>
    </xsl:when>
    <xsl:when test="@role = 'label' and not(empty(@cols))">
      <td colspan="{@cols}" style="font-weight:bold;"><xsl:apply-templates mode="text"/></td>
    </xsl:when>
    <xsl:when test="(@role = 'data' or empty(@role)) and empty(@cols)">
      <td><xsl:apply-templates mode="text"/></td>
    </xsl:when>
    <xsl:when test="(@role = 'data' or empty(@role)) and not(empty(@cols))">
      <td colspan="{@cols}"><xsl:apply-templates mode="text"/></td>
    </xsl:when>
    <xsl:otherwise>
      <td><xsl:apply-templates mode="text"/></td>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- TEI: dictionary    -->
<xsl:template match="entry" mode="text">
  <!-- empty: handled in text tag    -->
</xsl:template>

<xsl:template match="form" mode="text">
  <span class="form">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="orth" mode="text">
  <span class="orth">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="sense" mode="text">
  <span class="entryDiv"><xsl:apply-templates mode="text" select="def"/></span>
  <span class="entryDiv"><xsl:apply-templates mode="text" select="etym"/></span>
</xsl:template>

<xsl:template match="def" mode="text">
  <span class="def">
    <span class="bf"><xsl:value-of select="'Definition: '"/></span>
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="mentioned" mode="text">
  <span class="mentioned">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="etym" mode="text">
  <span class="etym">
    <span class="bf"><xsl:value-of select="'Etymology: '"/></span>
    <span class="entryDiv">
      <xsl:for-each select="cit">
        <li><xsl:value-of select="quote"/><xsl:value-of select="' ('"/><xsl:value-of select="def"/><xsl:value-of select="')'"/></li>
      </xsl:for-each>
    </span>
  </span>
</xsl:template>

<xsl:template match="cit" mode="text">
  <span class="cit">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<!-- TEI: MathML    -->
<xsl:template match="math:*" mode="text">
  <xsl:element name="{name()}" namespace="">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="text"/>
  </xsl:element>
</xsl:template>

<!-- TEI: SVG    -->
<xsl:template match="svg:*" mode="text">
  <xsl:element name="{name()}" namespace="">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="text"/>
  </xsl:element>
</xsl:template>

<xsl:template match="div" mode="text">
  <xsl:variable name="type" select="@type"/>
  <xsl:variable name="level" select="@level"/>
  <xsl:variable name="style" select="@style"/>
  <xsl:variable name="border" select="@border"/>
  <xsl:variable name="width" select="@width"/>
  <xsl:variable name="note" select="note"/>
  <xsl:variable name="figure" select="figure"/>
  <xsl:variable name="floatClassValue">
    <xsl:choose>
      <xsl:when test="not(empty($note))"><xsl:value-of select="''"/></xsl:when>
      <xsl:when test="not(empty($figure))"><xsl:value-of select="''"/></xsl:when>
      <xsl:when test="$type = 'float'"><xsl:value-of select="'float left'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'float left'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="levelClassValue">
    <xsl:choose>
      <xsl:when test="not(empty($note))"><xsl:value-of select="''"/></xsl:when>
      <xsl:when test="$level = '1'"><xsl:value-of select="'level one'"/></xsl:when>
      <xsl:when test="$level = '2'"><xsl:value-of select="'level two'"/></xsl:when>
      <xsl:when test="$level = '3'"><xsl:value-of select="'level three'"/></xsl:when>
      <xsl:when test="$level = '4'"><xsl:value-of select="'level four'"/></xsl:when>
      <xsl:when test="$level = '5'"><xsl:value-of select="'level five'"/></xsl:when>
      <xsl:when test="$level = '6'"><xsl:value-of select="'level six'"/></xsl:when>
      <xsl:when test="$level = '7'"><xsl:value-of select="'level seven'"/></xsl:when>
      <xsl:when test="$level = '8'"><xsl:value-of select="'level eight'"/></xsl:when>
      <xsl:when test="$level = '9'"><xsl:value-of select="'level nine'"/></xsl:when>
      <xsl:when test="empty($level)"><xsl:value-of select="''"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'level n'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <div>
    <xsl:if test="not(empty($style))"><xsl:attribute name="style"><xsl:value-of select="$style"/></xsl:attribute></xsl:if>
    <xsl:attribute name="class"><xsl:value-of select="concat($floatClassValue, ' ', $levelClassValue)"/></xsl:attribute>
    <xsl:apply-templates mode="text"/>
  </div>
</xsl:template>

<xsl:template match="p" mode="text">
  <xsl:variable name="currentHighlightElementPos">
    <xsl:choose>
      <xsl:when test="$highlightElement = 'p'"><xsl:value-of select="count(preceding::p[. >> $topPB]) + 1"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="0"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="style" select="@style"/>
  <xsl:variable name="class">
    <xsl:choose>
      <xsl:when test="not(empty(@style)) and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos"><xsl:value-of select="concat('p highlight ', @style)"/></xsl:when>
      <xsl:when test="empty(@style) and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos"><xsl:value-of select="'p highlight'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'p'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="not(empty($style))">
      <div>
        <xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
        <span>
          <xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
          <xsl:attribute name="id"><xsl:value-of select="concat('p', $currentHighlightElementPos)"/></xsl:attribute>
          <xsl:apply-templates mode="text"/>
        </span>
      </div>
    </xsl:when>
    <xsl:otherwise>
      <div>
        <xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
        <xsl:attribute name="id"><xsl:value-of select="concat('p', $currentHighlightElementPos)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </div>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="lb" mode="text">
  <xsl:variable name="withoutLBs">
    <xsl:choose>
      <xsl:when test="contains($options, 'withoutLBs')"><xsl:value-of select="'true'"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="'false'"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>  
  <xsl:if test="$withoutLBs = 'false'"><br/></xsl:if>
  <xsl:apply-templates mode="text"/>
</xsl:template>

<xsl:template match="cb" mode="text">
  <br/><xsl:apply-templates mode="text"/>
</xsl:template>

<xsl:template match="expan" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <span>
        <xsl:attribute name="class"><xsl:value-of select="concat('expan ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="expan"><xsl:apply-templates mode="text"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="note" mode="text">
  <xsl:variable name="hasLabel" select="string(@xlink:label) != ''"/>
  <xsl:variable name="place" select="@place"/>
  <xsl:variable name="notePos" select="count(preceding::note[. >> $topPB]) + 1"/>
  <xsl:variable name="href" select="concat('note-', $pageNumber, '-', $notePos)"/>
  <xsl:choose>
    <xsl:when test="$collectionName = 'archimedes'">
      <a>
        <xsl:attribute name="name"><xsl:value-of select="concat($href, 'ref')"/></xsl:attribute>
        <xsl:attribute name="href"><xsl:value-of select="concat(urlBase, '#', $href)"/></xsl:attribute>
        <xsl:attribute name="class"><xsl:value-of select="'noteRef super'"/></xsl:attribute>
        <xsl:value-of select="$href"/>
      </a>
    </xsl:when>
    <xsl:when test="$collectionName = 'echo' and not($hasLabel)">
      <p>
        <xsl:value-of select="'[Note]: '"/>
        <span class="note"><xsl:apply-templates mode="text"/></span>
      </p>
    </xsl:when>
    <xsl:when test="$collectionName = 'tei'">
      <xsl:choose>
        <xsl:when test="contains($place, 'margin')">
          <span>    
            <xsl:attribute name="class"><xsl:value-of select="concat('note ', $place)"/></xsl:attribute>
            <xsl:apply-templates mode="text"/>
          </span>
        </xsl:when>
        <xsl:otherwise>
          <a>
            <xsl:attribute name="name"><xsl:value-of select="concat($href, 'ref')"/></xsl:attribute>
            <xsl:attribute name="href"><xsl:value-of select="concat(urlBase, '#', $href)"/></xsl:attribute>
            <xsl:attribute name="class"><xsl:value-of select="'noteRef super'"/></xsl:attribute>
            <xsl:value-of select="$href"/>
          </a>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <span class="note"><xsl:apply-templates mode="text"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="emph" mode="text">
  <xsl:variable name="style" select="@style"/>
  <xsl:variable name="styleWithoutSC" select="replace($style, 'sc ', '')"/>
  <xsl:variable name="text" select="string-join(., '')"/>
  <xsl:variable name="length" select="string-length($text)"/>
  <xsl:variable name="firstChar" select="substring($text, 1, 1)"/>
  <xsl:variable name="first2Chars" select="substring($text, 1, 2)"/>
  <xsl:variable name="restChars" select="substring($text, 2, $length)"/>
  <xsl:variable name="first2CharsAreUppercase" select="upper-case($first2Chars) = $first2Chars"/>
  <xsl:variable name="rest">
    <xsl:choose>
      <xsl:when test="$length &lt; 2 or empty($length)"><xsl:value-of select="''"/></xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="not(empty(w))">
            <a class="textPollux" href="interface/lt/wordInfo.xql?language={w/@lang}&amp;display={$text}&amp;word={w/@form}&amp;output=html"><xsl:value-of select="$restChars"/></a>
          </xsl:when>
          <xsl:otherwise><xsl:value-of select="$restChars"/></xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$collectionName = 'echo' and not(contains($style, 'sc'))">
      <span class="{$style}"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:when test="$collectionName = 'echo' and $style = 'sc' and $length = 1">
      <span class="sc"><xsl:value-of select="$firstChar"/></span>
    </xsl:when>
    <xsl:when test="$collectionName = 'echo' and $style = 'sc' and $first2CharsAreUppercase">
      <span class="dc"><xsl:value-of select="$firstChar"/></span><span class="sc"><xsl:sequence select="$rest"/></span>
    </xsl:when>
    <xsl:when test="$collectionName = 'echo' and contains($style, 'sc') and $first2CharsAreUppercase">
      <span class="dc"><span class="{$styleWithoutSC}"><xsl:value-of select="$firstChar"/></span></span><span class="sc"><span class="{$styleWithoutSC}"><xsl:sequence select="$rest"/></span></span>
    </xsl:when>
    <xsl:when test="$collectionName = 'echo' and $style = 'sc' and not($first2CharsAreUppercase)">
      <span class="sc"><xsl:apply-templates mode="text"/></span>
    </xsl:when>
    <xsl:when test="$collectionName = 'echo' and contains($style, 'sc') and not($first2CharsAreUppercase)">
      <span class="sc"><span class="{$styleWithoutSC}"><xsl:apply-templates mode="text"/></span></span>
    </xsl:when>
    <xsl:when test="$collectionName = 'archimedes'">
      <xsl:apply-templates mode="text"/>
    </xsl:when>
    <xsl:otherwise><xsl:apply-templates mode="text"/></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="foreign" mode="text">
  <xsl:variable name="lang" select="@lang"/>
  <xsl:variable name="xmllang" select="@xml:lang"/>
  <xsl:variable name="language">
    <xsl:choose>
      <xsl:when test="not(empty($xmllang))"><xsl:value-of select="$xmllang"/></xsl:when>
      <xsl:when test="not(empty($lang))"><xsl:value-of select="$lang"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>  
  <span>
    <xsl:attribute name="class"><xsl:value-of select="concat('foreign ', $language)"/></xsl:attribute>
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="q" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <span>
        <xsl:attribute name="class"><xsl:value-of select="concat('q ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="q"><xsl:apply-templates mode="text"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="quote" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <div>
        <xsl:attribute name="class"><xsl:value-of select="concat('quote ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </div>
    </xsl:when>
    <xsl:otherwise>
      <div class="quote"><xsl:apply-templates mode="text"/></div>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="blockquote" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <div>
        <xsl:attribute name="class"><xsl:value-of select="concat('blockquote ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </div>
    </xsl:when>
    <xsl:otherwise>
      <div class="blockquote"><xsl:apply-templates mode="text"/></div>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="set-off" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <div>
        <xsl:attribute name="class"><xsl:value-of select="concat('set-off ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </div>
    </xsl:when>
    <xsl:otherwise>
      <div class="set-off"><xsl:apply-templates mode="text"/></div>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="reg" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <span>
        <xsl:attribute name="class"><xsl:value-of select="concat('reg ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="reg"><xsl:apply-templates mode="text"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="var" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <span>
        <xsl:attribute name="class"><xsl:value-of select="concat('reg ', @type, ' ', @style)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="var">
        <xsl:attribute name="class"><xsl:value-of select="concat('var ', @type)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="num" mode="text">
  <xsl:choose>
    <xsl:when test="not(empty(@style))">
      <span>
        <xsl:attribute name="class"><xsl:value-of select="'num'"/></xsl:attribute>
        <span>
          <xsl:attribute name="class"><xsl:value-of select="@style"/></xsl:attribute>
          <xsl:apply-templates mode="text"/>
        </span>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="num"><xsl:apply-templates mode="text"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="gap" mode="text">
  <xsl:variable name="extent" select="@extent"/>
  <xsl:variable name="count">
    <xsl:choose>
      <xsl:when test="empty($extent)"><xsl:value-of select="number(3)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="number($extent)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="gapChars" select="text:nchars($count, '.')"/>
  <xsl:value-of select="concat('[', $gapChars, ']')"/><xsl:apply-templates mode="text"/>
</xsl:template>

<xsl:template match="anchor" mode="text">
  <xsl:variable name="type" select="@type"/>
  <xsl:variable name="href" select="@xlink:href"/>
  <xsl:choose>
    <xsl:when test="$type = 'figure'">
      <xsl:variable name="figure" select="$figures/echo:figure[@xlink:label = $href]"/>
      <xsl:variable name="figureFileName">
        <xsl:choose>
          <xsl:when test="$collectionName = 'archimedes'"><xsl:value-of select="replace($figure/@xlink:href, '/', '.')"/></xsl:when>
          <xsl:when test="$collectionName = 'echo'"><xsl:value-of select="$figure/echo:image/@file"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$figure/echo:image/@file"/></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="figureNumber">
        <xsl:choose>
          <xsl:when test="$collectionName = 'archimedes'"><xsl:value-of select="$firstFigurePosition + count($figure/preceding::figure)"/></xsl:when>
          <xsl:when test="$collectionName = 'echo'"><xsl:value-of select="$firstFigurePosition + count(./preceding::figure[empty(@xlink:label)]) + count(./preceding::anchor[@type = 'figure'])"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$firstFigurePosition + count(./preceding::echo:figure[empty(@xlink:label)]) + count(./preceding::echo:anchor[@type = 'figure'])"/></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="figureCaption"><xsl:apply-templates mode="text" select="$figure/echo:caption"/></xsl:variable>
      <xsl:variable name="figureDescription">
        <xsl:for-each select="$figure/echo:description">
          <xsl:apply-templates mode="text" select="."/><br/>
        </xsl:for-each>
      </xsl:variable>
      <xsl:variable name="figureVariables"><xsl:apply-templates mode="text" select="$figure/echo:variables"/></xsl:variable>
      <xsl:sequence select="mpdl:showFigure($digilibAvailable, $figureFileName, $figureNumber, $figureCaption, $figureDescription, $figureVariables, 'float right')"/>
    </xsl:when>
    <xsl:when test="$type = 'handwritten'">
      <xsl:variable name="hw" select="$handwritten/echo:handwritten[@xlink:label = $href]"/>
      <xsl:variable name="hwFileName" select="$hw/@file"/>
      <xsl:variable name="hwHref" select="$hw/@xlink:href"/>
      <xsl:sequence select="mpdl:showHandwritten($hwFileName, $hwHref)"/>
    </xsl:when>
    <xsl:when test="$type = 'table'">
      <xsl:variable name="table" select="$tables/xhtml:table[@xlink:label = $href]"/>
      <xsl:sequence select="mpdl-util:copyWithoutNamespace($table)"/>
    </xsl:when>
    <xsl:when test="$type = 'note'">
      <a>
        <xsl:attribute name="name"><xsl:value-of select="concat($href, 'ref')"/></xsl:attribute>
        <xsl:attribute name="href"><xsl:value-of select="concat(urlBase, '#', $href)"/></xsl:attribute>
        <xsl:attribute name="class"><xsl:value-of select="'noteRef super'"/></xsl:attribute>
        <xsl:value-of select="$href"/>
      </a>
    </xsl:when>
    <xsl:otherwise><a><xsl:attribute name="href"><xsl:value-of select="$href"/></xsl:attribute><xsl:apply-templates mode="text"/></a></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- GIS Elements   -->
<xsl:template match="place" mode="text">
  <xsl:variable name="actualSN">
    <xsl:choose>
      <xsl:when test=". = $firstSentence and ($topPB >> .)">0</xsl:when>
      <xsl:otherwise><xsl:value-of select="count(preceding::s[. >> $topPB]) + 1"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="id" select="@id"/>
  <xsl:variable name="echoDocDir" select="replace($figuresImageDirectory, '(.*)/.*$', '$1')"/>
  <xsl:variable name="echoDocDirTokenized" select="tokenize($echoDocDir, '/')"/>
  <xsl:variable name="echoDocDirNameSize" select="count($echoDocDirTokenized)"/>
  <xsl:variable name="echoDoc" select="string(subsequence($echoDocDirTokenized, $echoDocDirNameSize))"/>
  <xsl:variable name="docStrOld" select="concat('http://echo.mpiwg-berlin.mpg.de/ECHOdocuView?url=', $echoDocDir, '&amp;pn=', $pageNumber, '&amp;sn=', $actualSN, '&amp;viewMode=gis')"/>
  <xsl:variable name="docStr" select="concat('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/page-query-result.xql?document=', $documentUri, '&amp;mode=gis', '&amp;pn=', $pageNumber, '&amp;sn=', $actualSN)"/>
  <xsl:variable name="docBase64Coded" select="saxon:string-to-base64Binary($docStr, 'utf8')"/>
  <xsl:variable name="href" select="concat('http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/db/mpdl/', $echoDoc, '?id=', $id, '&amp;doc=', $docBase64Coded, '&amp;format=gis')"/>
  <xsl:choose>
    <xsl:when test="$mode = 'textPollux'">
      <xsl:variable name="wordLanguage" select="string-join(w[1]/@lang, '')"/>
      <xsl:variable name="form" select="string-join(w/@form, '')"/>
      <xsl:variable name="wordStr">
        <xsl:if test="not(empty(w))">
          <xsl:value-of select="string-join(w, '')"/>
        </xsl:if>
        <xsl:if test="empty(w)">
          <xsl:value-of select="."/>
        </xsl:if>
      </xsl:variable>
      <xsl:variable name="lexHref">
        <xsl:if test="not(empty(w))">
          <xsl:value-of select="concat('interface/lt/wordInfo.xql?language=', $wordLanguage, '&amp;word=', $form, '&amp;output=html', '&amp;placeHref=', encode-for-uri($href))"/>
        </xsl:if>
        <xsl:if test="empty(w)">
          <xsl:value-of select="concat('interface/lt/wordInfo.xql?type=place', '&amp;language=', $language, '&amp;word=', $wordStr, '&amp;output=html', '&amp;placeHref=', encode-for-uri($href))"/>
        </xsl:if>
      </xsl:variable>
      <span class="place">
        <a class="place"><xsl:attribute name="href"><xsl:value-of select="$lexHref"/></xsl:attribute><xsl:sequence select="$wordStr"/></a>
      </span>
    </xsl:when>
    <xsl:when test="$mode = 'gis'">
      <a><xsl:attribute name="href"><xsl:value-of select="$href"/></xsl:attribute><xsl:apply-templates mode="text"/></a>
    </xsl:when>
    <xsl:otherwise>
      <span class="place">
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="time" mode="text">
  <span class="time">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="person" mode="text">
  <span class="person">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="event" mode="text">
  <span class="event">
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<!-- XHTML: remove the xhtml namespace   -->
<xsl:template match="xhtml:*" mode="text">
  <xsl:variable name="hasLabel" select="string(@xlink:label) != ''"/>
  <xsl:variable name="isTable" select="name() = 'table'"/>
  <xsl:choose>
    <xsl:when test="(not($hasLabel)) or ($isTable and $hasLabel)">
      <xsl:element name="{name()}" namespace="">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates mode="text"/>
      </xsl:element>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="figure" mode="text">
  <xsl:variable name="hasLabel" select="string(@xlink:label) != ''"/>
  <xsl:choose>
    <xsl:when test="not($hasLabel) and $collectionName != 'tei'">
      <xsl:variable name="figureFileName">
        <xsl:choose>
          <xsl:when test="$collectionName = 'archimedes'"><xsl:value-of select="replace(./@xlink:href, '/', '.')"/></xsl:when>
          <xsl:when test="$collectionName = 'echo'"><xsl:value-of select="./image/@file"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="./image/@file"/></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="figureNumber">
        <xsl:choose>
          <xsl:when test="$collectionName = 'archimedes'"><xsl:value-of select="$firstFigurePosition + count(./preceding::figure)"/></xsl:when>
          <xsl:when test="$collectionName = 'echo'"><xsl:value-of select="$firstFigurePosition + count(./preceding::figure[empty(@xlink:label)]) + count(./preceding::anchor[@type = 'figure'])"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$firstFigurePosition + count(./preceding::figure[empty(@xlink:label)]) + count(./preceding::anchor[@type = 'figure'])"/></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="figureCaption"><xsl:apply-templates mode="text" select="caption"/></xsl:variable>
      <xsl:variable name="figureDescription">
        <xsl:for-each select="description">
          <xsl:apply-templates mode="text" select="."/><br/>
        </xsl:for-each>
      </xsl:variable>
      <xsl:variable name="figureVariables"><xsl:apply-templates mode="text" select="variables"/></xsl:variable>
      <xsl:sequence select="mpdl:showFigure($digilibAvailable, $figureFileName, $figureNumber, $figureCaption, $figureDescription, $figureVariables, 'float none')"/>
    </xsl:when>
    <xsl:when test="not($hasLabel) and $collectionName = 'tei'">
      <xsl:variable name="figureNumber" select="$firstFigurePosition + count(./preceding::figure)"/>
      <xsl:choose>
        <xsl:when test="@facs != ''">
          <xsl:sequence select="mpdl:showFigure($digilibAvailable, @facs, $figureNumber, '', '', '', 'float none')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="href" select="graphic/@url"/>
          <xsl:variable name="head" select="head"/>
          <xsl:variable name="figDesc" select="string(figDesc)"/>
          <div class="figure" style="margin-left:10px;">
            <a href="{$href}"><img alt="Figure: {$figDesc}" src="{$href}" width="200" height="200"/></a>
            <br/>
            <xsl:value-of select="concat('[Figure ', $figureNumber, ']: ')"/><xsl:value-of select="$head"/>
          </div>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="caption" mode="text">
  <span class="figureCaption"><xsl:apply-templates mode="text"/></span>
</xsl:template>

<xsl:template match="description" mode="text">
  <span class="figureDescription"><xsl:apply-templates mode="text"/></span>
</xsl:template>

<xsl:template match="handwritten" mode="text">
  <xsl:variable name="hasLabel" select="string(@xlink:label) != ''"/>
  <xsl:choose>
    <xsl:when test="not($hasLabel)">
      <xsl:variable name="fileName" select="@file"/>
      <xsl:variable name="href" select="@xlink:href"/>
      <xsl:sequence select="mpdl:showHandwritten($fileName, $href)"/>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- textPollux links                           -->
<xsl:template match="w" mode="text">
  <xsl:variable name="wordLanguage" select="@lang"/>
  <xsl:variable name="form" select="encode-for-uri(@form)"/>
  <xsl:variable name="displayWord"><xsl:apply-templates mode="text"/></xsl:variable>
  <xsl:variable name="displayWordUrlEncoded" select="encode-for-uri($displayWord)"/>
  <xsl:choose>
    <xsl:when test="not(empty(anchor))">
      <xsl:for-each select="node()">
        <xsl:variable name="dispWord"><xsl:apply-templates mode="text" select="."/></xsl:variable>
        <xsl:variable name="dispWordUrlEncoded" select="encode-for-uri($dispWord)"/>
        <xsl:choose>
          <xsl:when test=". instance of text()">
            <a class="textPollux">
              <xsl:attribute name="href"><xsl:value-of select="concat('interface/lt/wordInfo.xql?language=', $wordLanguage, '&amp;display=', $dispWordUrlEncoded, '&amp;word=', $form, '&amp;output=html')"/></xsl:attribute>
              <xsl:sequence select="$dispWord"/>
            </a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:sequence select="$dispWord"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <a class="textPollux">
        <xsl:attribute name="href"><xsl:value-of select="concat('interface/lt/wordInfo.xql?language=', $wordLanguage, '&amp;display=', $displayWordUrlEncoded, '&amp;word=', $form, '&amp;output=html')"/></xsl:attribute>
        <xsl:sequence select="$displayWord"/>
      </a>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="s" mode="text">
  <xsl:variable name="style" select="@style"/>
  <xsl:variable name="currentSN">
    <xsl:choose>
      <xsl:when test=". = $firstSentence and ($topPB >> .)">0</xsl:when>
      <xsl:otherwise><xsl:value-of select="count(preceding::s[. >> $topPB]) + 1"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <!-- Show the sentence in color light grey if it is given as sn or highlightElem -->
    <xsl:when test="($sn >= 0 and $sn = $currentSN) or ($highlightElement = 's' and $highlightElementPos >= 0 and $highlightElementPos = $currentSN)">
      <span class="s highlight" id="s{$currentSN}">
        <xsl:if test="not(empty(@xmlNodeId))"><xsl:attribute name="xmlNodeId"><xsl:value-of select="@xmlNodeId"/></xsl:attribute></xsl:if>
        <xsl:choose>
          <xsl:when test="empty(@style)">
            <xsl:apply-templates mode="text"/>
          </xsl:when>
          <xsl:otherwise>
            <span class="{$style}"><xsl:apply-templates mode="text"/></span>
          </xsl:otherwise>
        </xsl:choose>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="s" id="s{$currentSN}">
        <xsl:if test="not(empty(@xmlNodeId))"><xsl:attribute name="xmlNodeId"><xsl:value-of select="@xmlNodeId"/></xsl:attribute></xsl:if>
        <xsl:choose>
          <xsl:when test="empty(@style)">
            <xsl:apply-templates mode="text"/>
          </xsl:when>
          <xsl:otherwise>
            <span class="{$style}"><xsl:apply-templates mode="text"/></span>
          </xsl:otherwise>
        </xsl:choose>
      </span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- If ft-query is set then highlight all term occurrences in each little text piece for the fulltext query -->
<xsl:template match="text()" mode="text">
  <xsl:variable name="ancestorStr" select="concat('./ancestor::', $highlightElement)"/>
  <xsl:variable name="highlightElementAncestor">
    <xsl:choose>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:sequence select="./ancestor::s"/></xsl:when>
      <xsl:otherwise><xsl:sequence select="saxon:evaluate($ancestorStr)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="highlightElementAncestorType" select="name($highlightElementAncestor/*[1])"/>
  <xsl:variable name="countPrecedingElemsStr" select="concat('count(preceding::', $highlightElement, '[. >> $p1]) + 1')"/>
  <xsl:variable name="currentHighlightElementPos">
    <xsl:choose>
      <xsl:when test="($highlightElement = 's' or $highlightElement = '') and $highlightElementAncestor = $firstSentence and ($topPB >> $highlightElementAncestor)">0</xsl:when>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:value-of select="saxon:evaluate($countPrecedingElemsStr, $topPB)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="saxon:evaluate($countPrecedingElemsStr, $topPB)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$ftQueryMode != 'false' and $highlightElementAncestorType = $highlightElement and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos">
      <xsl:sequence select="text:highlight(string(.), $ftQueryTerms, $ftQueryHighlightWords, 'false')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>