Skip to content

handle decision on `cei:date` and `cei:dateRange` for chronological ordering in a common function

Created by: GVogeler

As https://github.com/icaruseu/mom-ca/blob/master/my/XRX/src/mom/app/charters/charters.xqm#L80-L83 handle the same problem as the solution of https://github.com/icaruseu/mom-ca/pull/751 I would suggest to move the solution from https://github.com/icaruseu/mom-ca/issues/679#issuecomment-342526594 into a common function in charters.xqm:

declare function charters:date-selector($cei_issued, $range_limit as xs:string?) as xs:string {
 (: 
The function decides which dating element is the most expressive in a cei:issued structure:
$cei_issued is the cei:issued element which contains either cei:date/@value or cei:dateRange/(@from|@to) or both. 
$range_limit indicates if the function should return the lower or the upper value of cei:dateRange
Strategy is: 
1. the lower value is preferred (to get the dates below the generic "unknown" value '99999999')
2. if that does not help cei:date is preferred against cei:dateRange
:)
    let $rl := if(count($range_limit) = 0) then ('from') else ($range_limit)
    let $date := $cei_issued/(
      cei:date[
        not(../cei:dateRange) 
        or xs:integer(@value) lt xs:integer(../cei:dateRange/@*[name()=$range_limit])
        or xs:integer(@value) = xs:integer(../cei:dateRange/@*[name()=$range_limit])
        ]/@value
      |
      cei:dateRange[
        not(../cei:date) 
        or xs:integer(@*[name()=$range_limit]) lt xs:integer(../cei:date/@*[name()=$range_limit])
        ]/@*[name()=$range_limit]
    )
    return $date[1]/string()
};