xquery version "%(xquery_version)s";
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(),
    $url   := normalize-space($res/%(resource_url)s),
    $url_img := normalize-space($res//lomfrens:ensDocumentLabel/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>{$url}</url>
        <urlLeadImage>{$url_img}</urlLeadImage>
        <publisher>{$acad}</publisher>
  </res>
