I have several lists which may or may not be empty. I want to find those elements which occur in all lists, but only for those lists that are not empty.
I have something like this:
let $results :=
$list1 intersect
$list2 intersect
$list3 intersect
$list4
But if any of the lists is empty this expression returns an empty list. Is there any way I can exclude a list from my intersection if it is empty?
SOLUTION:
This is the solution I ended up using, based on the answer provided by Ranon.
let $union := $list1 | $list2 | $list3 | $list4
let $results :=
(if ($list1) then $list1 else $union) intersect
(if ($list2) then $list2 else $union) intersect
(if ($list3) then $list3 else $union) intersect
(if ($list4) then $list4 else $union)
I would like to thank all who have contributed. Coming from an object-oriented and procedural background, and with XQuery being a functional language it doesn’t come as naturally to me (yet).
You could check all lists if they’re empty. If so, assign the union of all lists to them.
For my example I used the functx-implementation of intersect and union to be able to intersect sequences, too. Maybe one should write some function to avoid redundant code, but for showing the idea this code is fine:
$unioncould also be written as($list1, $list2, $list3), but that will lead to double elements which result in slower intersection-operations for large element counts.