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

let $date_debut := current-date() - xs:dayTimeDuration("P183D")
(: ~6mois :)

for $res in collection('%(meta_url)s')/lom:lom[lom:lifeCycle/lom:contribute[lom:role/lom:value='publisher']//lom:dateTime >= $date_debut][lom:lifeCycle/lom:contribute[lom:role/lom:value='publisher']//lom:dateTime <= adjust-date-to-timezone(current-date(),())]
let $date_aff:= $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()), ", "),
    $author := string-join(distinct-values($res/lom:lifeCycle/lom:contribute[lom:role/lom:value = "author"]/lom:entity), "| "),
    $url   := normalize-space($res/%(resource_url)s),
    $url_img := normalize-space($res//lomfrens:ensDocumentLabel/text()),
    $titre := $res/lom:general/lom:title/lom:string/text(),
    $description := $res/lom:general/lom:description/lom:string/text()

order by $date_aff descending empty least
return
  <res>
  <title>{$titre}</title>
  <description>{$description}</description>
  <url>{$url}</url>
  <urlLeadImage>{$url_img}</urlLeadImage>
  <pubdate>{$date_aff}</pubdate>
  <type>{$rscType}</type>
  <author>{$author}</author>
</res>
