Is there a better way? Getting earliest date

View: New views
3 Messages — Rating Filter:   Alert me  

Is there a better way? Getting earliest date

by PencilEd :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I was able to get this to work with the code below -

Basically, I'm given a date that looks like this:
        <ms:Dates>
                <ms:Date ms:type="posted" ms:value="2008-07-23"/>
        </ms:Dates>

I've a bunch in my collection, but need to get the year (for starters) of the earliest one... so, while I'm almost certain there's a better way (and hoping someone can teach me), I'm doing this:

---Code---
declare function analysis:earliest() as xs:integer {
  let $ordered_dates := for $the_dates in collection($collection_all )/ms:Resource/ms:Dates/ms:Date
                                order by $the_dates/@ms:value
                                return $the_dates
  let $min_date := $ordered_dates[1]
  let $year_string := substring($min_date/@ms:value, 1, 4)
  return xs:integer($year_string)
};
---End Code---

Re: Is there a better way? Getting earliest date

by Adam Retter-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I would probably change how you obtain the year to use year-from-date
function - e.g. -

year-from-date($min_date/@ms:value)




2009/6/25 PencilEd <gstewart@...>:

>
> I was able to get this to work with the code below -
>
> Basically, I'm given a date that looks like this:
>        <ms:Dates>
>                <ms:Date ms:type="posted" ms:value="2008-07-23"/>
>        </ms:Dates>
>
> I've a bunch in my collection, but need to get the year (for starters) of
> the earliest one... so, while I'm almost certain there's a better way (and
> hoping someone can teach me), I'm doing this:
>
> ---Code---
> declare function analysis:earliest() as xs:integer {
>  let $ordered_dates := for $the_dates in collection($collection_all
> )/ms:Resource/ms:Dates/ms:Date
>                                order by $the_dates/@ms:value
>                                return $the_dates
>  let $min_date := $ordered_dates[1]
>  let $year_string := substring($min_date/@ms:value, 1, 4)
>  return xs:integer($year_string)
> };
> ---End Code---
> --
> View this message in context: http://www.nabble.com/Is-there-a-better-way---Getting-earliest-date-tp24205954p24205954.html
> Sent from the exist-open mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Exist-open mailing list
> Exist-open@...
> https://lists.sourceforge.net/lists/listinfo/exist-open
>



--
Adam Retter

eXist Developer
{ United Kingdom }
adam@...
irc://irc.freenode.net/existdb

------------------------------------------------------------------------------
_______________________________________________
Exist-open mailing list
Exist-open@...
https://lists.sourceforge.net/lists/listinfo/exist-open

Re: Is there a better way? Getting earliest date

by PencilEd :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, Adam.

That helped, but I guess I was hoping for something more like a min() for dates... like this:

let $min_date := min(collection($collection)/Dates/Date/value)

or, better... something more like:

let $min_date := collection($collection)/Dates/Date/value/[. = min(.)]