view software/eXist/webapp/mpdl/_stuff/oxygen-projects/monte-project/archimedes-text-fragment.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
line wrap: on
line source

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:xlink="http://www.w3.org/1999/xlink">

<xsl:output method="html"/>

<xsl:variable name="ftQueryName" select="/result/ft-query/name"/>
<xsl:variable name="ftQueryStr" select="concat('&amp;', 'ft-query=', $ftQueryName)"/>
<xsl:variable name="ftQueryMode">
  <xsl:choose>
  <xsl:when test="matches($ftQueryName, concat('&quot;', '.*', '&quot;'))">
    <xsl:value-of select="'phrase'"/>
  </xsl:when>
  <xsl:when test="string-length($ftQueryName) = 0">
    <xsl:value-of select="'false'"/>
  </xsl:when>
  <!-- Because there are  problems in recognizing words (in mode word) this fake mode is used (same as phrase mode)  -->
  <xsl:otherwise><xsl:value-of select="'fakeWord'"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<xsl:variable name="ftQueryValue">
  <xsl:choose>
  <xsl:when test="/result/ft-query[node()]"><xsl:value-of select="$ftQueryStr"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:template match="result">
  <html><body>
    <xsl:for-each select="document-description">
      <h2>
      <xsl:for-each select="info">
        <xsl:value-of select="author"/>.
        <xsl:value-of select="title"/>.
        <xsl:value-of select="place"/>, 
        <xsl:value-of select="date"/>
      </xsl:for-each>
      </h2>
    </xsl:for-each>
    <xsl:for-each select="page">
      <xsl:variable name="documentName" select="/result/document-description/document-name"/>
      <xsl:variable name="countPages" select="/result/document-description/count-pages"/>
      <xsl:variable name="number" select="number(number)"/>
      <xsl:variable name="documentValue" select="concat('document=', $documentName)"/>
      <xsl:variable name="pnValue" select="concat('pn=', $number)"/>
      <xsl:variable name="modeImageValue" select="concat('mode=', 'image')"/>
      <xsl:variable name="modeXmlValue" select="concat('mode=', 'xml')"/>
      <xsl:variable name="modeTextValue" select="concat('mode=', 'text')"/>
      <xsl:variable name="imageLink" select="concat($documentValue, '&amp;', $pnValue, '&amp;', $modeImageValue, $ftQueryValue)"/>
      <xsl:variable name="textLink" select="concat($documentValue, '&amp;', $pnValue, '&amp;', $modeTextValue, $ftQueryValue)"/>
      <xsl:variable name="xmlLink" select="concat($documentValue, '&amp;', $pnValue, '&amp;', $modeXmlValue, $ftQueryValue)"/>
      <form action="" method="get"></form>
      <table width="100%">
        <colgroup>
          <col width="30%"/>
          <col width="70%"/>
        </colgroup>
        <tr>
        <td align="left" nowrap="true">
          [Text] 
          [<a href="?{$imageLink}">Image</a>]
          [<a href="?{$xmlLink}">XML</a>] 
        </td>
        <td valign="right" nowrap="true">
          <td>
            <xsl:choose>
              <xsl:when test="$number &gt; 1">
                <a href="?{$documentValue}&amp;pn={$number - 1}&amp;{$modeTextValue}{$ftQueryValue}"><img src="images/left.gif" alt="page-down" border="0"/></a>
              </xsl:when>
              <xsl:otherwise>
                <a href="?{$textLink}"><img src="images/left.gif" alt="page-down" border="0"/></a>
              </xsl:otherwise>
            </xsl:choose>
          </td>
          <td nowrap="true">
            <xsl:value-of select="$number"/> / <xsl:value-of select="$countPages"/> 
          </td>
          <td>
            <xsl:choose>
              <xsl:when test="$number &lt; $countPages">
                <a href="?{$documentValue}&amp;pn={$number + 1}&amp;{$modeTextValue}{$ftQueryValue}"><img src="images/right.gif" alt="page-up" border="0"/></a>
              </xsl:when>
              <xsl:otherwise>
                <a href="?{$textLink}"><img src="images/right.gif" alt="page-down" border="0"/></a>
              </xsl:otherwise>
            </xsl:choose>
          </td>
          <td nowrap="true">
            <input type="hidden" name="document" value="{$documentName}"/>
            Page: <input type="text" size="3" name="pn" value="{$number}"/>
            <input type="hidden" name="mode" value="text"/>
            <xsl:if test="/result/ft-query[node()]">
              <input type="hidden" name="ft-query" value="{$ftQueryName}"/>
            </xsl:if>
          </td>
        </td>
        </tr>
      </table>
      <hr/>
      <table align="middle" width="100%">
        <colgroup>
          <col width="70%"/>
          <col width="30%"/>
        </colgroup>
        <tr>
          <td align="left" valign="top">
            <xsl:for-each select="content">
              <xsl:apply-templates/>
            </xsl:for-each>
          </td>
          <xsl:if test="$ftQueryValue!=''">
          <td align="left" valign="top">
            <b><xsl:value-of select="/result/ft-query/result/size"/> Hits: "<xsl:value-of select="$ftQueryName"/>"</b>
            <ol>
              <xsl:for-each select="/result/ft-query/result/hits/hit">
              <li>
                <xsl:variable name="hitPN" select="pn"/>
                <xsl:variable name="hitPosOfS" select="pos-of-s"/>
                <a href="?{$documentValue}&amp;pn={$hitPN}&amp;{$modeTextValue}{$ftQueryValue}">Page <xsl:value-of select="$hitPN"/>, Sentence <xsl:value-of select="$hitPosOfS"/></a>
              </li>
              </xsl:for-each>
            </ol>
          </td>
          </xsl:if>
        </tr>
      </table>
      <hr/>
      Elapsed time:  <xsl:value-of select="/result/document-description/performance"/> ms, Back to <a href="query.xql">query page</a>, see the <a href="page-query-result.xql?_source=yes">XQuery source</a> and <a href="/exist/rest/db/xsl/archimedes-text-fragment.xsl?_source=yes">XSL source</a> of this page
      <br/>
      [<a href="page-query-result.xql?{$textLink}">fast mechanism (file system search)</a>] [<a href="page-query-result-old.xql?{$textLink}">slower mechanism (XQL search)</a>]
    </xsl:for-each>
  </body></html>
</xsl:template>

<xsl:template match="chap|p">
  <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="lb">
  <br/>
</xsl:template>

<xsl:template match="expan">
  <xsl:apply-templates/><xsl:text> </xsl:text>
</xsl:template>

<xsl:variable name="firstFigurePosition" select="/result/page/firstFigurePosition"/>

<xsl:template match="figure">
  <xsl:variable name="figurePos" select="count(preceding::figure)"/>
  <xsl:variable name="figureId" select="$firstFigurePosition + $figurePos"/>
  <xsl:variable name="documentName" select="/result/document-description/document-name"/>
  <!-- delivers all fileds from 021/01/001/1.jpg -->
  <xsl:variable name="figureLink" select="@xlink:href"/>
  <xsl:variable name="docId" select="substring-before($figureLink, '/')"/>
  <xsl:variable name="figureLinkAfter1" select="substring-after($figureLink, '/')"/>
  <xsl:variable name="docId2" select="substring-before($figureLinkAfter1, '/')"/>
  <xsl:variable name="figureLinkAfter2" select="substring-after($figureLinkAfter1, '/')"/>
  <xsl:variable name="page" select="substring-before($figureLinkAfter2, '/')"/>
  <xsl:variable name="figureFileName" select="substring-after($figureLinkAfter2, '/')"/>
  <p></p>
  <a href="http://echo.mpiwg-berlin.mpg.de/zogilib?fn=/permanent/archimedes/{$documentName}/{$docId}-{$docId2}-figures&amp;pn={$figureId}"><img alt="figure: {$figureId}" src="http://nausikaa2.rz-berlin.mpg.de/digitallibrary/servlet/Scaler?fn=/permanent/archimedes/{$documentName}/{$docId}-{$docId2}-figures&amp;pn={$figureId}&amp;dh=150" align="middle" border="1"/></a>
  <p></p>
  Figure: <xsl:value-of select="$figureId"/>
  <p></p>
</xsl:template>

<!-- Highlight all term occurrences for the fulltext query. It recognizes all text nodes and then does the highlighting -->
<xsl:template match="text()">
  <xsl:choose>
  <xsl:when test="$ftQueryMode != 'false'">
    <xsl:variable name="queryExpr" select="$ftQueryName"/>
    <xsl:call-template name="highlightQueryTerms">
    <xsl:with-param name="elemTextContent" select="."/>
    <xsl:with-param name="queryExprStr" select="$queryExpr"/>
    <xsl:with-param name="mode" select="$ftQueryMode"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="."/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- Highlight term occurrences in one text node. It recognizes the first occorrence in a text node and then does 
this template recursive with the first substring before the first term cut off -->
<xsl:template name="highlightQueryTerms">
  <xsl:param name="elemTextContent"/>
  <xsl:param name="queryExprStr"/>
  <xsl:param name="mode"/>
  <!-- Translation from Lucene fulltext query to text query  -->  
  <xsl:variable name="textQuery">
    <xsl:choose>
    <xsl:when test="$mode = 'phrase'">
      <xsl:value-of select="substring-before(substring-after($queryExprStr, '&quot;'), '&quot;')"/>
    </xsl:when>
    <xsl:when test="$mode = 'fakeWord'">
      <xsl:value-of select="$queryExprStr"/>
    </xsl:when>
    <xsl:when test="$mode = 'word'">
      <xsl:value-of select="$queryExprStr"/>
    </xsl:when>
    <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <!-- Word delimiter -->
  <xsl:variable name="wordDelimRegExpr" select="'[\s,:\.^ ]+'"/>  
  <!-- Recognizes the beginning of the line with ^ and the substring up to the query term -->
  <xsl:variable name="queryRegExprSubstringBefore">
    <xsl:choose>
    <xsl:when test="$mode = 'phrase'">
      <xsl:value-of select="concat('^.*?', $textQuery)"/>
    </xsl:when>
    <xsl:when test="$mode = 'fakeWord'">
      <xsl:value-of select="concat('^.*?', $textQuery)"/>
    </xsl:when>
    <xsl:when test="$mode = 'word'">
      <xsl:value-of select="concat('^.*?', $wordDelimRegExpr, $textQuery, '(',  $wordDelimRegExpr, ')')"/>
    </xsl:when>
    <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <!-- Recognizes the substring after the query term -->
  <xsl:variable name="queryRegExprSubstringAfter">
    <xsl:choose>
    <xsl:when test="$mode = 'phrase'">
      <xsl:value-of select="concat($textQuery, '.*')"/>
    </xsl:when>
    <xsl:when test="$mode = 'fakeWord'">
      <xsl:value-of select="concat($textQuery, '.*')"/>
    </xsl:when>
    <xsl:when test="$mode = 'word'">
      <xsl:value-of select="concat('(', $wordDelimRegExpr, ')', $textQuery, $wordDelimRegExpr, '.*')"/>
    </xsl:when>
    <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <!-- Deletes the substring up to the query term -->
  <xsl:variable name="substringBefore" select="replace($elemTextContent, $queryRegExprSubstringAfter, '$1', 'i')"/>
  <!-- Deletes the substring after the query term -->
  <xsl:variable name="substringAfter" select="replace($elemTextContent, $queryRegExprSubstringBefore, '$1', 'i')"/>
  <xsl:choose>
    <xsl:when test="matches($elemTextContent, $queryRegExprSubstringBefore, 'i')">
      <!-- Prints the original part of the substring up to the first occurrence of the query term -->
      <xsl:value-of select="$substringBefore"/>
      <!-- Highlight the query term -->
      <xsl:variable name="matchQueryTermRegExpr">
        <xsl:choose>
        <xsl:when test="$mode = 'phrase'">
          <xsl:value-of select="concat('^.*?', '(', $textQuery, ')', '.*')"/>
        </xsl:when>
        <xsl:when test="$mode = 'fakeWord'">
          <xsl:value-of select="concat('^.*?', '(', $textQuery, ')', '.*')"/>
        </xsl:when>
        <xsl:when test="$mode = 'word'">
          <xsl:value-of select="concat('^.*?', $wordDelimRegExpr, '(', $textQuery, ')', $wordDelimRegExpr, '.*')"/>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="''"/></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="matchQueryTerm" select="replace($elemTextContent, $matchQueryTermRegExpr, '$1', 'i')"/>
      <span style="background-color: yellow;">
        <xsl:value-of select="$matchQueryTerm"/>
      </span>
      <!-- Recursive call of this template with the substring after the first occurrence of the term: further occurrences of the query 
      term  -->
      <xsl:call-template name="highlightQueryTerms">
        <xsl:with-param name="elemTextContent" select="$substringAfter"/>
        <xsl:with-param name="queryExprStr" select="$queryExprStr"/>
        <xsl:with-param name="mode" select="$mode"/>
      </xsl:call-template>
    </xsl:when>
    <!-- if no occurrence of the query term could be found the whole string is printed -->
    <xsl:otherwise>
      <xsl:value-of select="$elemTextContent"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


</xsl:stylesheet>