<t:switch prioritized="true">
  <div role="main">
    { $inOrderings := get("inOrderings", false()) or contains(@class, "template-name-holds") (:template-name-checkedout:),
    
      let $submitBtn := //input[@name = ("renewSelected", "cancelSelected")]
      let $form := $submitBtn/ancestor::form[1]
      let $form := form($form, ($submitBtn, "confirm=1"))
      return 
        if ($inOrderings) then $cancel-form := $form
        else $renew-form := $form
    }
 
    <t:element t:condition="(self::div or self::li) and starts-with(@id, 'record')"> 
       <t:s>
       ((ancestor-or-self::div[contains-token(@class, "row")])[last()],.)[1]/(
       let $checkbox := .//input[@type="checkbox" and not(@disabled)] return
            $book := {"status": join ( (.//div)[last()][not(strong)] | .//div[contains(@class, "alert")] ), 
                      "statusId": if ($inOrderings) then "ordered" 
                                  else if ($checkbox) then "curious" 
                                  else "critical",
                      if ($inOrderings) then "_cancelID" else "_renewID": $checkbox/@value,
                      "title": (.//a[contains(@class, "title")], .//span[@class = "title"])[1],
                      "author": (.//a[contains(@href, 'type=Author') or contains(@href, '/Author/')])[1]
                     },
        (.//strong, .//label) ! (
        let $field := . || following-sibling::text()[1]
        let $sep := if (contains($field, ":")) then ":" else " "
        let $name := normalize-space(substring-before($field, $sep))
        let $value := normalize-space(substring-after($field, $sep))
        let $value := if (ends-with($value, ",")) then substring($value, 1, string-length($value) - 1) else $value
        return if (empty(switch ($name)
(:          case "von" return if (empty($book.author)) then $book.author := $value else ():)
          case "Signatur" case "Call Number" return $book.id := $value
          case "due date" case "fällig am" case "Bis" case "until" case "Ablaufdatum" case "Fälligkeit am" case "Rückgabedatum" case "Rückgabe bis einschließlich" return 
            if (matches($value, "[0-9][-0-9.]{7,10}")) then $book.duedate := parse-date($value) else ()
          case "Erstellt" case "Created" case "Ausleihdatum" return 
            if (matches($value, "[0-9][-0-9.]{7,10}")) then $book.issuedate := parse-date($value) else ()
          case "Zweigstelle" case "Borrowing Location" case "Pickup library" return $book.libraryBranch := $value
          case "Regalstandort" return $book.libraryLocation := $value
          case "Strichcode" case "Buchungsnummer" case "barcode" case "Barcode" return $book.barcode := $value
          case "Medientyp" case "Media type" return $book.id := $value
          case "Status" case "Vormerkstatus" return (
            if ($inOrderings) then $book.statusId := "ordered" else (), 
            extract(join((following-sibling::text(), ..)), "[0-9][-0-9.]{7,10}")[.] ! ( $book.duedate := parse-date(.) ),
            $book.status := $value
          )
          case "Erscheinungsjahr" return $book.year := $value
          case "Ausleihstelle" case "Abholort" case "Pickup Location" return $book.libraryBranch := $value
          default return ())
        ) then
          if (empty($book.title)) then (
            let $split := extract($field, "(.*)\[(.*)\]", (1,2))
            return if (empty($split)) then $book.title := $field
            else ($book.title := $split[1], $book.author := $split[2])
          ) else $book($name || "!") := $value
          else ()
       ) ,
        if (empty($book.title[.])) then $book.title := join(.//br[1]/(preceding-sibling::strong,preceding-sibling::text())) else ()
      )
      </t:s>
    </t:element>+
  </div>
  
  <div role="main" t:condition="empty(.//input[contains(@id, 'IDS')])">
  </div>
  
</t:switch>
