xquery version "1.0";
declare namespace lom="http://ltsc.ieee.org/xsd/LOM";
declare namespace lomfrens="http://pratic.ens-lyon.fr/xsd/LOMFRENS";

for $res in collection("%(COLLECTION)s")/lom:lom[contains(lom:lifeCycle/lom:contribute[lom:role/lom:value='publisher']/lom:date/lom:dateTime/text(), "%(DATE_ARCHIVES)s")][lom:lifeCycle/lom:contribute[lom:role/lom:value='publisher']//lom:dateTime <= adjust-date-to-timezone(current-date(),())]
let $date := $res/lom:lifeCycle/lom:contribute[lom:role/lom:value='publisher']/lom:date/lom:dateTime/text(),
    $rscType := string-join(distinct-values($res/lomfrens:ensData/lomfrens:ensDocumentType/lomfrens:value/text()), ", "),
    $urlRs:= substring-after(document-uri(root($res)), '/LOM-'),
    $titre := $res/lom:general/lom:title/lom:string/text(),
    $description:= $res/lom:general/lom:description/lom:string/text(),
    $location:= $res/lom:technical/lom:location[1]/text(),
    $author:= string-join(distinct-values($res/lom:lifeCycle/lom:contribute[lom:role/lom:value = "author"]/lom:entity), "| ")
order by $date descending empty least
return
    <res>
      <title>{$titre}</title>
      <description>{$description}</description>
      <url>{if(starts-with($location, 'xmldb:exist://'))
    then
      concat('%(site_url)s/XML', document-uri(root($res)))
    else
      $location
    }</url>
      <pubdate>{$date}</pubdate>
      <type>{$rscType}</type>
      <author>{$author}</author>
    </res>