Last active
August 14, 2021 08:13
-
-
Save joewiz/8349d75a658fa52fc72c3b4f4c108a8d to your computer and use it in GitHub Desktop.
Get the most recently created document in an eXist collection, using XQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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