view software/eXist/webapp/mpdl/interface/xpath.xql @ 15:e99964f390e4

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

xquery version "1.0";

declare namespace request = "http://exist-db.org/xquery/request";
declare namespace util = "http://exist-db.org/xquery/util";
declare namespace echo="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/";
declare namespace TEI="http://www.tei-c.org/ns/1.0";

(: ToDo wenn eine pn gegeben ist, wird momentan der namespace ignoriert  :)
let $mpdlDocUri := request:get-parameter("document", "")
let $pn := number(request:get-parameter("pn", "-1"))
let $xpath := request:get-parameter("xpath", "")
let $output := request:get-parameter("output", "")
let $resultPageNumber := number(request:get-parameter("resultPN", "1"))
let $resultPageSize := number(request:get-parameter("resultPageSize", "50"))

let $documentName := substring-before(substring-after(substring-after(substring-after($mpdlDocUri, "/"), "/"), "/"), ".")
let $docbase := substring-before(substring-after($mpdlDocUri, "/"), "/")
let $fullDocumentUri := concat('/db/mpdl/documents/morph', $mpdlDocUri)
let $document := doc($fullDocumentUri)
let $pageBreaks := 
  if ($docbase = 'archimedes' and $pn != -1)
  then $document//pb
  else if ($docbase = 'echo' and $pn != -1)
  then $document//echo:pb
  else if ($docbase = 'tei')
  then $document//TEI:pb
  else $document//pb
let $countPBs := count($pageBreaks)
let $pb1 := subsequence($pageBreaks, $pn, 1)
let $pb2 := subsequence($pageBreaks, $pn + 1, 1)
let $pageFragmentStr := 
  if ($pn != -1 and $countPBs > 0)
  then util:get-fragment-between($pb1, $pb2, true())
  else if ($pn != -1 and $countPBs = 0)
  then ""
  else ()
let $pageFragment := 
  if ($countPBs > 0)
  then util:parse($pageFragmentStr)
  else $document
let $xPathFragment := concat(".", $xpath)
let $resultEval :=
  if ($pn > 0)
  then util:eval-inline($pageFragment, $xPathFragment)
  else util:eval-inline($document, $xPathFragment)
let $from := ($resultPageNumber * $resultPageSize) - $resultPageSize + 1
let $to := $resultPageNumber * $resultPageSize
let $resultEvalPage := 
  for $entry at $pos in $resultEval
  where $pos >= $from and $pos <= $to
  return $entry
let $size := count($resultEval)
let $pages := 
  if ($size = 0)
  then 0
  else $size idiv $resultPageSize + 1
let $result := 
      <result>
        <document>{$mpdlDocUri}</document>
        <documentPageNumber>{$pn}</documentPageNumber>
        <resultSize>{$size}</resultSize>
        <resultPageSize>{$resultPageSize}</resultPageSize>
        <countResultPages>{$pages}</countResultPages>
        <resultPageNumber>{$resultPageNumber}</resultPageNumber>
        <resultPage>{$resultEvalPage}</resultPage>
      </result>  

let $declare := 
  if ($output != "html")
  then util:declare-option("exist:serialize", "method=xml media-type=text/xml omit-xml-declaration=no indent=yes encoding=utf-8")
  else util:declare-option("exist:serialize", "method=html media-type=text/html omit-xml-declaration=no indent=yes encoding=utf-8")

return $result