Skip to content

Instantly share code, notes, and snippets.

@joewiz
Last active August 14, 2021 08:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewiz/8349d75a658fa52fc72c3b4f4c108a8d to your computer and use it in GitHub Desktop.
Save joewiz/8349d75a658fa52fc72c3b4f4c108a8d to your computer and use it in GitHub Desktop.
Get the most recently created document in an eXist collection, using XQuery
xquery version "3.1";
(: See discussion at http://markmail.org/message/hpu7toznx3fvdiei :)
import module namespace util="http://exist-db.org/xquery/util";
import module namespace xmldb="http://exist-db.org/xquery/xmldb";
declare function local:get-latest-created-document($collection-uri as xs:string) as map(*) {
if (xmldb:collection-available($collection-uri)) then
let $documents := xmldb:xcollection($collection-uri) ! util:document-name(.)
(: to include all resources, not just documents, change this expression to:
xmldb:get-child-resources($collection-uri)
to include all documents in descendant collections, change this expression to:
collection($collection-uri) ! util:document-name(.)
:)
return
if (exists($documents)) then
let $latest-created :=
(
$documents
=> sort((), xmldb:created($collection-uri, ?))
)[last()]
return
map {
"collection-uri": $collection-uri,
"document-name": $latest-created,
"created": xmldb:created($collection-uri, $latest-created)
}
else
map {
"warning": "No child documents in collection " || $collection-uri
}
else
map {
"warning": "No such collection " || $collection-uri
}
};
let $collection-uri := "/db/apps/eXide"
return
local:get-latest-created-document($collection-uri)
map {
"created": xs:dateTime("2017-11-09T13:01:58.099-05:00"),
"collection-uri": "/db/apps/eXide",
"document-name": "repo.xml"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment