(: : Calendar functions :) module "http://www.w3.org/2003/05/xpath-functions" define variable $d1m as xdt:yearMonthDuration { xdt:yearMonthDuration("P1M") } define variable $d1d as xdt:dayTimeDuration { xdt:dayTimeDuration("P1D") } (: : The weekday for dates >= 1900-01-01 : return number 0:Monday .. 6:Sunday :) define function weekday($date as xs:date) as xs:integer { let $start-date := xs:date("1900-01-01"), (: first monday :) $n-days := get-days-from-dayTimeDuration($date - $start-date) return $n-days mod 7 } (: : The weekday name :) define function weekday-name($wday as xs:integer) as xs:string { let $wdays := ( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ) return $wdays[ $wday + 1 ] } (: : The month name :) define function month-name($month as xs:integer) as xs:string { let $months := ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ) return $months[ $month ] } (: : The first day of a month :) define function first-day-of-month($date as xs:date) as xs:date { xs:date(concat(substring(xs:string($date), 1, 8), "01")) } (: : The last day of a month :) define function last-day-of-month($date as xs:date) as xs:date { let $first := first-day-of-month($date) return $first + $d1m - $d1d } (: : A day in the month :) define function day-of-month($date as xs:date, $day as xs:integer) as xs:date { let $first := first-day-of-month($date) return $first + xdt:dayTimeDuration(concat('P', xs:string($day - 1), 'D')) } (: : Same day, one month before :) define function previous-month($date as xs:date) as xs:date { xs:date($date - $d1m) } (: : Same day, one month after :) define function next-month($date as xs:date) as xs:date { xs:date($date + $d1m) } (: : Small montly calendar :) define function print-small-calendar($date as xs:date, $uri as xs:string) { let $day := get-day-from-date($date), $first := first-day-of-month($date), $last := last-day-of-month($date), $wd-first := weekday($first), $first-i := $wd-first + 1, $last-i := $wd-first + get-day-from-date($last), $events := doc($uri)/events/event[date ge $first and date le $last] return { for $i in 0 to 6 return } { for $wl in ( 1, 8, 15, 22, 29, 36 ) return if ($wl <= $last-i) then { for $wi in $wl to $wl + 6 let $d := if ($wi < $first-i or $wi > $last-i ) then 0 else $wi - $wd-first return if ($d > 0) then else else () }
< { concat( month-name(get-month-from-date($date)), " ", xs:string(get-year-from-date($date))) } >
{ substring(weekday-name($i), 1, 2) }
{ if ($d = $day ) then attribute class { "day" } else () } { let $day-event := $events[date eq day-of-month($date, $d)] return if ($day-event) then { $d } else $d } }
} (: : Big montly calendar :) define function print-large-calendar($date as xs:date, $uri as xs:string) { let $day := get-day-from-date($date), $first := first-day-of-month($date), $last := last-day-of-month($date), $wd-first := weekday($first), $first-i := $wd-first + 1, $last-i := $wd-first + get-day-from-date($last), $events := doc($uri)/events/event[date ge $first and date le $last] return { for $i in 0 to 6 return } { for $wl in ( 1, 8, 15, 22, 29, 36 ) return if ($wl <= $last-i) then { for $wi in $wl to $wl + 6 let $d := if ($wi < $first-i or $wi > $last-i ) then 0 else $wi - $wd-first return if ($d > 0) then else } else () }
{ let $previous := previous-month($date) return { month-name(get-month-from-date($previous)) } } { concat( month-name(get-month-from-date($date)), " ", xs:string(get-year-from-date($date))) } { let $next := next-month($date) return { month-name(get-month-from-date($next)) } }
{ weekday-name($i) }
{ if ($d = $day ) then attribute class { "lday" } else () } { $d } { let $day-event := $events[date eq day-of-month($date, $d)] return if ($day-event) then
{ $day-event/title/node() }
else () }
 
}