I have discovered what appears to be a problem with recursive (or at least re-entering) XQuery functions. It doesn't happen all the time, in fact most of the simple test cases I could come up with seemed to work fine. However, the XQuery code below doesn't behave as expected or as it behaves in Stylus Studio. It is designed to report the number of weekdays between two dates. If the start date is less than the end date it works fine (you can check the answer in Excel using the NETWORKDAYS function). If the start date is greater, it should return the negative difference. Sure enough, the first number returned in the example case is 4 in both BaseX and Stylus Studio. However, the second number is -1 in BaseX when I was expecting -4.
Thanks for looking into this,
Dave
declare namespace functx = "http://www.functx.com"; declare function functx:day-of-week ( $date as xs:anyAtomicType? ) as xs:integer? { if (empty($date)) then () else xs:integer((xs:date($date) - xs:date('1901-01-06')) div xs:dayTimeDuration('P1D')) mod 7 }; declare function local:weekdays ($start as xs:anyAtomicType?, $end as xs:anyAtomicType?) as xs:integer? { if(empty($start) or empty($end)) then() else if($start > $end) then -local:weekdays($end, $start) else let $dayOfWeekStart := functx:day-of-week($start) let $dayOfWeekEnd := functx:day-of-week($end) let $adjDayOfWeekStart := if($dayOfWeekStart = 0) then 7 else $dayOfWeekStart let $adjDayOfWeekEnd := if($dayOfWeekEnd = 0) then 7 else $dayOfWeekEnd return if($adjDayOfWeekStart <= $adjDayOfWeekEnd) then xs:integer((xs:integer(days-from-duration(xs:date($end) - xs:date($start)) div 7) * 5) + max(((min((($adjDayOfWeekEnd + 1), 6)) - $adjDayOfWeekStart), 0))) else xs:integer((xs:integer(days-from-duration(xs:date($end) - xs:date($start)) div 7) * 5) + min((($adjDayOfWeekEnd + 6) - min(($adjDayOfWeekStart, 6)), 5))) }; (local:weekdays('2010-06-14', '2010-06-17'), local:weekdays('2010-06-17', '2010-06-14'))