view software/eXist/webapp/mpdl/presentation/pageFragmentHtml.xsl @ 7:5589d865af7a

Erstellung XQL/XSL Applikation
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 08 Feb 2011 15:16:46 +0100
parents
children d2a1c14fde31
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:mml="http://www.w3.org/1998/Math/MathML" 
  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: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="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="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: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"/>
         <xsl:if test="$contentStr = '' and empty($figures)">
           <div class="emptyPage"><xsl:value-of select="'[Empty page]'"/></div>
         </xsl:if>
         <xsl:choose>
         <xsl:when test="$mode = 'text' or $mode = 'textPollux' or $mode = 'gis'">
             <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>

<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="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="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:choose>
    <!-- Show the sentence in color light grey if it is given as sn -->
    <xsl:when test="$elementName = 's' and $sn >= 0 and $sn = $actualSN">
      <ul class="xml element highlight">
        <a name="sn{$actualSN}"></a><xsl:sequence select="$elementPresentation"/>
      </ul>
    </xsl:when>
    <xsl:when test="$elementName = 's' and $sn != $actualSN">
      <ul class="xml element">
        <a name="sn{$actualSN}"></a><xsl:sequence select="$elementPresentation"/>
      </ul>
    </xsl:when>
    <xsl:otherwise>
      <ul class="xml element">
        <xsl:sequence select="$elementPresentation"/>
      </ul>
    </xsl:otherwise>
  </xsl:choose>
</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>

<xsl:template match="text()" mode="xml">
  <xsl:variable name="parentS" select="./ancestor::s"/>
  <xsl:variable name="actualSN">
    <xsl:choose>
      <xsl:when test="$parentS = $firstSentence and ($topPB >> $parentS)">0</xsl:when>
      <xsl:otherwise><xsl:value-of select="count(preceding::s[. >> $topPB]) + 1"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$highlightQuery != '' and $sn >= 0 and $sn = $actualSN">
      <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"/>
  <!--   Notes                      -->
  <xsl:if test="$collectionName = 'archimedes' and count($notes/*) > 0">
    <div>
      <hr class="notesBottom"/>
      <xsl:for-each select="$notes/note">
        <xsl:variable name="notePos" select="position()"/>
        <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(.)"/>
        <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: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: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:value-of select="concat('[↑ ', $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">
  <p class="bf center"><xsl:apply-templates mode="text"/></p>
</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="style" select="@style"/>
  <p>
    <xsl:if test="not(empty($style))">
      <xsl:attribute name="class"><xsl:value-of select="$style"/></xsl:attribute>
    </xsl:if>
    <xsl:apply-templates mode="text"/>
  </p>
</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:apply-templates mode="text"/><xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="note" mode="text">
  <xsl:variable name="hasLabel" select="string(@xlink:label) != ''"/>
  <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="'super'"/></xsl:attribute>
        <xsl:value-of select="concat(' ↓ (', $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:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="emph" mode="text">
  <xsl:variable name="class" select="@class"/>
  <xsl:variable name="style" select="@style"/>
  <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="lt/wordInfo.xql?language={w/@lang}&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:if test="$collectionName = 'echo' and not(contains($class, 'sc'))"><span class="{$class} {$style}"><xsl:apply-templates mode="text"/></span></xsl:if>
  <xsl:if test="$collectionName = 'echo' and contains($class, 'sc') and $first2CharsAreUppercase"><span class="dc {$style}"><xsl:value-of select="$firstChar"/></span><span class="{$class} {$style}"><xsl:sequence select="$rest"/></span></xsl:if>
  <xsl:if test="$collectionName = 'echo' and contains($class, 'sc') and not($first2CharsAreUppercase)"><span class="{$class} {$style}"><xsl:apply-templates mode="text"/></span></xsl:if>
  <xsl:if test="$collectionName = 'archimedes'"><xsl:apply-templates mode="text"/></xsl:if>
</xsl:template>

<xsl:template match="ref" mode="text">
  <span class="ref"><xsl:apply-templates mode="text"/></span>
</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">
  <div class="q"><xsl:apply-templates mode="text"/></div>
</xsl:template>

<xsl:template match="quote" mode="text">
  <div class="quote"><xsl:apply-templates mode="text"/></div>
</xsl:template>

<xsl:template match="blockquote" mode="text">
  <div class="blockquote"><xsl:apply-templates mode="text"/></div>
</xsl:template>

<xsl:template match="set-off" mode="text">
  <div class="set-off"><xsl:apply-templates mode="text"/></div>
</xsl:template>

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

<xsl:template match="var" mode="text">
  <xsl:variable name="type" select="@type"/>
  <span class="var">
    <xsl:attribute name="class"><xsl:value-of select="concat('var ', $type)"/></xsl:attribute>
    <xsl:apply-templates mode="text"/>
  </span>
</xsl:template>

<xsl:template match="num" mode="text">
  <span class="num"><xsl:apply-templates mode="text"/></span>
</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" select="string-join($figure/echo:caption/text(), ' ')"/>
      <xsl:variable name="figureDescription" select="string-join($figure/echo:description/text(), ' ')"/>
      <xsl:variable name="figureVariables" select="string-join($figure/echo:variables/text(), ' ')"/>
      <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="'super'"/></xsl:attribute>
        <xsl:value-of select="concat('↓ (', $href, ')')"/>
      </a>
    </xsl:when>
    <xsl:otherwise><a><xsl:attribute name="href"><xsl:value-of select="@xlink:href"/></xsl:attribute>Anchor of type: <xsl:value-of select="@type"/>, href: <xsl:value-of select="@xlink:href"/></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" select="string-join(w, '')"/>
      <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;output=html', '&amp;placeHref=', encode-for-uri($href))"/>
        </xsl:if>
      </xsl:variable>
      <span class="place">
        <a class="textPollux"><xsl:attribute name="href"><xsl:value-of select="$lexHref"/></xsl:attribute><xsl:value-of select="$wordStr"/></a>
      </span>
    </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>

<!-- MML   -->
<xsl:template match="mml:*" mode="text">
  <xsl:copy-of select="."/>
</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)">
      <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" select="./caption/text()"/>
      <xsl:variable name="figureDescription" select="./description/text()"/>
      <xsl:variable name="figureVariables" select="./variables/text()"/>
      <xsl:sequence select="mpdl:showFigure($digilibAvailable, $figureFileName, $figureNumber, $figureCaption, $figureDescription, $figureVariables, 'float none')"/>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</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="@form"/>
  <a class="textPollux">
    <xsl:attribute name="href"><xsl:value-of select="concat($baseUrlLex, 'lt/wordInfo.xql?language=', $wordLanguage, '&amp;word=', $form, '&amp;output=html')"/></xsl:attribute>
    <xsl:apply-templates mode="text"/>
  </a>
</xsl:template>

<xsl:template match="s" 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>
  <a name="sn{$actualSN}"></a>
  <xsl:choose>
    <!-- Show the sentence in color light grey if it is given as sn -->
    <xsl:when test="$sn >= 0 and $sn = $actualSN">
      <span class="s highlight">
        <xsl:if test="contains($options, 'withXmlNodeId')"><xsl:attribute name="xmlNodeId"><xsl:value-of select="@xmlNodeId"/></xsl:attribute></xsl:if>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="s">
        <xsl:if test="contains($options, 'withXmlNodeId')"><xsl:attribute name="xmlNodeId"><xsl:value-of select="@xmlNodeId"/></xsl:attribute></xsl:if>
        <xsl:apply-templates mode="text"/>
      </span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template match="text()" mode="text">
  <xsl:variable name="parentS" select="./ancestor::s"/>
  <xsl:variable name="actualSN">
    <xsl:choose>
      <xsl:when test="$parentS = $firstSentence and ($topPB >> $parentS)">0</xsl:when>
      <xsl:otherwise><xsl:value-of select="count(preceding::s[. >> $topPB]) + 1"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
  <xsl:when test="$highlightQuery != '' and $sn >= 0 and $sn = $actualSN">
    <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>