1
votes

I am quite new to Marklogic and xquery in general. We have a collection of documents which have a deadline date as a node. Can somebody help me with an xquery to get the latest document based on deadline date? Each document looks like

<document>
<Id>blah<Id>
<DeadlineDate>2012-04-04T21:00:00</DeadLineDate>
1
Provide some valid XML as input please. Do you actually use XQuery collections? You want the document with latest deadline (would have expected the first)?Jens Erat

1 Answers

2
votes

You will need a range index on DeadlineDate, of type dateTime.

let $latest := cts:element-values(
  xs:QName('DeadlineDate'), ('document', 'descending', 'limit=1'))
return /document[DeadLineDate eq $latest]

Or you could also use this form:

(for $n in /document[DeadLineDate]
 order by $n/DeadLineDate descending
 return $n)[1]

I think the first one will usually be faster, but that might be worth testing.