for $y in document('v-depts.xml')/departments/department where $y/deptno/date(@tend) >= date('1999-01-01') and $y/deptno/date(@tstart) <= date('1999-01-01') return (element department{$y/deptno, element counting{count( for $x in document('v-emps.xml')/employees/employee where $x/deptno/text() = $y/deptno/text() and $x/deptno/date(@tend) >= date('1999-01-01') and $x/deptno/date(@tstart) <= date('1999-01-01') return $x)}})