view software/eXist/webapp/mpdl/presentation/pageFragmentHtml.xsl @ 17:7e883ce72fec

diverse Fehlerbehebungen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Sep 2011 16:41:15 +0200
parents e99964f390e4
children
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="/mpdl/presentation/functions-mpdl.xsl" />
<xsl:import href="/mpdl/presentation/functions-text.xsl" />
<xsl:import href="/mpdl/presentation/functions-util.xsl" />

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

<xsl:variable name="mode" select="/result/page/mode"/>
<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="highlightQuery" select="string(/result/page/highlights/query)"/>
<xsl:variable name="highlightQueryTermsTemp" select="string-join(text:translateLuceneToTerms($highlightQuery), '')" as="xs:string"/>
<xsl:variable name="highlightQueryWords" select="string(/result/page/highlights/words)"/>
<xsl:variable name="highlightQueryTerms" select="text:removeDuplicates($highlightQueryTermsTemp, $highlightQueryWords)"/> 

<xsl:template match="result">
  <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="countPlaces" select="/result/document-description/count-places"/>
    <xsl:variable name="countTocEntries" select="/result/document-description/count-toc-entries"/>
    <xsl:variable name="countFigureEntries" select="/result/document-description/count-figure-entries"/>
    <xsl:variable name="pageHeader" select="header"/>
    <xsl:variable name="pageNumber" select="number(number)"/>
    <xsl:variable name="pageNumberOrig" select="number-orig"/>
    <xsl:variable name="pageNumberOrigNorm" select="number-orig-norm"/>
    <xsl:variable name="documentValue" select="concat('document=', $documentUri)"/>
    <xsl:variable name="pnValue" select="concat('pn=', $pageNumber)"/>
    <xsl:variable name="modeValue" select="concat('mode=', $mode)"/>
	<div class="page">
	<div class="pageMeta">
	  <div class="countPages"><xsl:value-of select="$countPages"/></div>
	  <div class="countPlaces"><xsl:value-of select="$countPlaces"/></div>
	  <div class="countTocEntries"><xsl:value-of select="$countTocEntries"/></div>
	  <div class="countFigureEntries"><xsl:value-of select="$countFigureEntries"/></div>
	  <div class="pageNumber"><xsl:value-of select="$pageNumber"/></div>
      <!-- test ob die Zahl gerade oder ungerade ist     -->
      <xsl:choose>
        <xsl:when test="$pageNumberOrig = ''"></xsl:when>
        <xsl:otherwise><div class="pageNumberOrig"><xsl:value-of select="$pageNumberOrig"/></div></xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
        <xsl:when test="$pageNumberOrigNorm = ''"></xsl:when>
        <xsl:otherwise><div class="pageNumberOrigNorm"><xsl:value-of select="$pageNumberOrigNorm"/></div></xsl:otherwise>
      </xsl:choose>
      <xsl:if test="$pageHeader != ''">
        <div class="pageHeaderTitle"><xsl:value-of select="$pageHeader"/></div>
      </xsl:if>
	</div>
    <div class="pageContent">
       <xsl:for-each select="content">
         <xsl:variable name="contentStr" select="normalize-space(string(.))"/>
         <xsl:variable name="figures" select=".//figure|.//handwritten|.//anchor[@type='figure']"/>
         <xsl:if test="$contentStr = '' and empty($figures)">
           <div class="emptyPage"><xsl:value-of select="'[Empty page]'"/></div>
         </xsl:if>
         <xsl:variable name="firstContentNodeName" select="./*[1]/name()"/>
         <xsl:choose>
           <xsl:when test="($mode = 'text' or $mode = 'textPollux' or $mode = 'gis') and $firstContentNodeName = 'html'">
             <xsl:apply-templates select=".//body" mode="html"/>
           </xsl:when>
           <xsl:when test="($mode = 'text' or $mode = 'textPollux' or $mode = 'gis') and $firstContentNodeName != 'html'">
             <xsl:apply-templates mode="text"/>
           </xsl:when>
           <xsl:when test="$mode = 'xml'">
             <xsl:apply-templates mode="xml"/>
           </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"/>
             <xsl:choose>
               <xsl:when test="$digilibAvailable = 'true' and $imageAvailable = 'true'">
                 <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>
               </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>
           </xsl:when>
           <xsl:otherwise></xsl:otherwise>
         </xsl:choose>
       </xsl:for-each>
    </div>
    </div>
  </xsl:for-each>
</xsl:template>

<!-- html mode: transform html texts -->
<xsl:template match="@*|node()" mode="html">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="html"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="s" mode="html">
  <xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="w" mode="html">
  <xsl:apply-templates select="." mode="text"/>
</xsl:template>

<!-- xml mode: transform to browser like xml display -->
<xsl:template match="element()" 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() != ''">
      <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:template>

<xsl:template match="comment()" mode="xml">
  <span class="xml comment">
    <xsl:value-of select="'&lt;!-- '"/><xsl:value-of select="."/><xsl:value-of select="' --&gt;'"/>
  </span>
</xsl:template>

<xsl:template match="processing-instruction()" mode="xml">
</xsl:template>

<xsl:template match="text()" mode="xml">
  <xsl:variable name="highlightElementAncestor">
    <xsl:choose>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:sequence select="./ancestor::s"/></xsl:when>
      <xsl:when test="$highlightElement = 'head'"><xsl:sequence select="./ancestor::head"/></xsl:when>
      <xsl:otherwise><xsl:sequence select="./ancestor::s"/></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="$highlightQuery != '' and $highlightElementAncestorType = $highlightElement and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos">
      <xsl:sequence select="text:highlight(string(.), $highlightQueryTerms, $highlightQueryWords, '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="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">
    <xsl:choose>
      <xsl:when test="$collectionName = 'archimedes' or $collectionName = 'tei'">
        <xsl:value-of select="count($notes/note[contains(@place, 'bottom')])"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="count($notes/note[contains(@position, 'bottom') or empty(string(@position))])"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <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>
      <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/echo:note">
        <xsl:variable name="label" select="string(@xlink:label)"/>
        <xsl:variable name="place" select="@position"/>
        <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="$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:when>
          <xsl:otherwise></xsl:otherwise>
         </xsl:choose>
      </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:if test="not(empty(@identifier))"><xsl:attribute name="identifier"><xsl:value-of select="@identifier"/></xsl:attribute></xsl:if>
      <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:when test="not(empty(@style))"><xsl:value-of select="concat('p ', @style)"/></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|br" 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">
    <xsl:choose>
      <xsl:when test="$collectionName = 'tei'"><xsl:value-of select="@place"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="@position"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <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) and not(empty($place))">
      <span>    
        <xsl:attribute name="class"><xsl:value-of select="concat('note margin ', $place)"/></xsl:attribute>
        <xsl:apply-templates mode="text"/>
      </span>
    </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 = 'echo' and $hasLabel"></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="http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/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'">
      <xsl:variable name="note" select="$notes/echo:note[@xlink:label = $href]"/>
      <xsl:variable name="noteWithoutNamespace" select="mpdl-util:copyWithoutNamespace($note)"/>
      <xsl:variable name="place" select="$note/@position"/>
      <xsl:choose>
        <xsl:when test="not(empty($place))">
          <span>    
            <xsl:attribute name="class"><xsl:value-of select="concat('note margin ', $place)"/></xsl:attribute>
            <xsl:apply-templates select="$noteWithoutNamespace/node()" 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><a><xsl:attribute name="href"><xsl:value-of select="@xlink: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="baseUrlLex" select="'http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/'"/>
      <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($baseUrlLex, '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($baseUrlLex, '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:value-of 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="baseUrlLex" select="'http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/'"/>
  <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($baseUrlLex, '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($baseUrlLex, '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 highlightElementPos -->
    <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:if test="not(empty(@identifier))"><xsl:attribute name="identifier"><xsl:value-of select="@identifier"/></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:if test="not(empty(@identifier))"><xsl:attribute name="identifier"><xsl:value-of select="@identifier"/></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>


<xsl:template match="text()" mode="text">
  <xsl:variable name="highlightElementAncestor">
    <xsl:choose>
      <xsl:when test="$highlightElement = 's' or $highlightElement = ''"><xsl:sequence select="./ancestor::s"/></xsl:when>
      <xsl:when test="$highlightElement = 'head'"><xsl:sequence select="./ancestor::head"/></xsl:when>
      <xsl:otherwise><xsl:sequence select="./ancestor::s"/></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="$highlightQuery != '' and $highlightElementAncestorType = $highlightElement and $highlightElementPos >= 0 and $highlightElementPos = $currentHighlightElementPos">
      <xsl:sequence select="text:highlight(string(.), $highlightQueryTerms, $highlightQueryWords, 'false')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>