xquery version "1.0";
declare default collation "?lang=fr-FR;strength=primary;decomposition=none";
declare namespace lom="http://ltsc.ieee.org/xsd/LOM";
declare namespace text="http://exist-db.org/xquery/text";
declare namespace lomfrens="http://pratic.ens-lyon.fr/xsd/LOMFRENS";

for $res in collection("%(COLLECTION)s")/lom:lom[lom:classification[1]/lom:taxonPath%(PARTS)s[lom:source/lom:string="%(CLASSIFICATION_NAME)s"]]%(TERMES)s%(OBJECTS)s%(CONDITION_BASE)s%(CONDITION_SUP)s
let $title       := $res/lom:general/lom:title/lom:string/text(),
    $description := $res/lom:general/lom:description/lom:string/text(),
    $author := string-join(distinct-values($res/lom:lifeCycle/lom:contribute[lom:role/lom:value = "author"]/lom:entity),"| "),
    $date        := $res/lom:lifeCycle/lom:contribute[lom:role/lom:value='publisher'][1]/lom:date/lom:dateTime/text(),
    $location    := $res/lom:technical/lom:location[1]/text(),
    $acad        := $res/lom:lifeCycle/lom:contribute[lom:role/lom:value = "publisher"][1]/lom:entity/text(),
    $type        := string-join(distinct-values($res/lomfrens:ensData/lomfrens:ensDocumentType/lomfrens:value/text()),", ")
order by %(TRI)s empty least
return
    <res>
        <title>{$title}</title>
        <description>{$description}</description>
        <author>{$author}</author>
        <pubdate>{$date}</pubdate>
        <type>{$type}</type>
        <url>{if(starts-with($location, 'xmldb:exist://'))
        then
          concat('%(site_url)s/XML', document-uri(root($res)))
        else
          $location
        }</url>
        <publisher>{$acad}</publisher>
  </res>
