<html>

<template:meta default-text-matching="regex"/>
<t:s>curAuthor := (), holding-keys := (), holding := (), if (get("singleResult", false())) then book:={} else ()</t:s> <!-- extra variable to prevent contamination by previously set book.author -->


<table summary="Main layout">
  
  {let $labels := //table/(. | ./tbody)/tr/td[contains(@class, 'preslabel')]
   return ($labels-ok := count($labels) > 0,
   let $unusedLabelChars := " .:" || codepoints-to-string((9,10,13,160))
   for $label in $labels
   let $name := replace($label, "^ +|:.*$", "")
   let $value-td := $label/following-sibling::td
   let $value := string($value-td)
   return 
   switch ($name)
     case "Titel" return $book.title := extract($value, '[^/]+')
     case "Verfasser" case "Personen" return (
       curAuthor := ($curAuthor, extract($value, '[^*]+') ),
       book.author := string-join($curAuthor, "; ") )
     case "ISBN" return $book.isbn := $value
     case "PPN" return 
       $book.home-url := resolve-html($value-td//a)
     case "Zeitschrift" return (
       $book.title := concat(if (contains($value, ":")) then substring-before($value, ":") else $value, 
                            if (contains($book.id, "-")) then concat(" [",substring-after($book.id, "-"), "]") else ""),
       $book.author := if (contains($value, "/")) then x"{substring-after(normalize-space($value), "/")} (Zeitschrift)" else "Zeitschrift" 
     )
     case "Körperschaft" return
       if ($book.author = "Zeitschrift") then book.author := concat($value, " (Zeitschrift)") else ()
     case "Standort" case "Signatur" case "status" case "Ausleihstatus" case "Bestand" case "Vorhanden in" return (
       if ($holding-keys = $name ) then ( $book.holdings[] := $holding, $holding-keys := (), $holding := {"orderable": false()} )
       else if (empty($holding)) then $holding := {"orderable": false()} else (),
       $holding-keys := ($holding-keys, $name),
       switch ($name) 
         case "Standort" case "Vorhanden in" return $holding.libraryBranch := $value
         case "Signatur" return $holding.id := $value
         case "Anmerkung"  return $holding("Anmerkung!") := $value
         case "Bestand"  return $holding("Bestand!") := $value
         case "status" case "Ausleihstatus" return 
           if (exists($holding("libraryBranch"))) then 
             $value-td! (
               $holding.status := string-join(div/text(), '; '),
               let $script := .//script return (
               $holding.orderable := exists($script),
                 $holding._orderId := extract($script, '(/(loan|LBS_WEB)[^'']*)\\''', 1),
                 $holding.orderTitle := extract($script, ">[^a-zA-Z]*([a-zA-Z]*)&lt;/a>", 1)
               )
             ) else ()
         default return ()
     )
     case "Anmerkung" return 
       if (exists($holding)) then $holding("Anmerkung!") := $value else $book("Anmerkung!") := $value
     case "" return ()
     default return
       $book($name||"!") :=  (
         let $link := $value-td//a
         return if ($link and (starts-with($name, "URL") or starts-with($name, "URN") or contains($value, "Volltext") )) then x"{$value} ( {resolve-html($link)} )"
         else $value
       ),
     if (exists($holding)) then $book.holdings[] := $holding else ()
  ),
  
  
  let $longproperties := .//tr[td[@class="longkey"]]/[.//td[@class="longkey"], td[@class="longval"] ]
    where exists($longproperties)
    return $book.holdings := (
      for tumbling window $w in $longproperties start $s when empty($s(2)//hr) end next $n when exists($n(2)//hr)
      return (
        {| $w / {.(1) || "!": .(2)/join((., .//a/x" ( {@href} ) "))},
         {"orderable": false()} |}
      )
    )
  }
  
  <template:switch-prioritized>
  <table t:condition="$labels-ok"/>
  
  <strong>Leider keine Treffer.</strong>
  <font t:condition="contains(@style, 'red')">Leider keine Treffer.</font>
  
  <!-- for un-unique results -->
  <table summary="hitlist">
      
    <tr valign="top">
      <td></td> <!-- icon -->
      <td></td> <!-- number -->
      <td class="hit">
        <a>{book.title := deep-text()}</a>
        <br/>
        {book.author := extract(text(), " */? *-? *([^-]*)", 1) ,
                       (: replace(text(), "( *([-,.])? *\[?(ca[.] *)?[0-9]{4}( *- *)?([0-9]{4})?\]?)$", "") :)
         book.year   := extract(text(),             "(\[?(ca[.] *)?[0-9]{4}( *- *)?([0-9]{4})?\]?)$", 1)
        } 
      </td>
    </tr>
  </table>
      
  
  </template:switch-prioritized>
  
  <img t:condition="contains(@src, 'permalink')">  <!-- permalink.gif, or permalink_du.gif, ... -->
  {book.home-url:=if (contains(../@href, "://")) then ../@href else concat(get("base", ""),"/",../@href)}
  </img>?
  
  
</table>



</html>
