Reputation: 435
Given 2 xml file that contain a list of number in each.
<list1>
<item>1</item>
<item>2</item>
...
</list1>
list1 = [1,2,3,4,5,7,8,9,10]
list2 = [1,4,6,2,8]
I only want the number in list1 that are greater than all of the number in list2, how can I do that?
I tried this, but it only return the number if it is greater than one of the number in list2 and not the entire list.
for $x in doc("list1.xml")/item
for $y in doc("list2.xml")/item
where ($x > $y)
return $x
Thanks in advance, newbie in xquery.
Upvotes: 1
Views: 1206
Reputation: 3517
Perhaps I have misunderstood the question, but it seems to me that there is a much simpler and more efficient solution using a single predicate which does not require any explicit loops:
xquery version "1.0";
let $list1 := (1, 2, 3, 4, 5, 7, 8, 9, 10)
let $list2 := (1, 4, 6, 2, 8)
let $max2 := max($list2)
return
$list1[. gt $max2]
I would also like to make it clear to @ou-ye a little about XQuery terminology. At present there is no such thing as a List structure in XQuery, the closest thing is a Sequence which is constructed using (
and )
.
Upvotes: 0
Reputation: 6218
Using XQuery 3.0 you can use fn:for-each
, which makes a nicer syntax in my opinion. So for each item in $list1
it checks whether there is an item in $list2
which is larger.
let $list1 := (1 to 10)
let $list2 := (1, 4, 6, 2, 8)
return for-each($list1, function($l) {
if (for-each($list2, function($x) {$l > $x}) = false())
then ()
else $l
})
If you just have XQuery 1.0, this should work as well and does basically the same:
let $list1 := (1 to 10)
let $list2 := (1, 4, 6, 2, 8)
for $l in $list1
where not((
for $f in $list2
return $l > $f
) = false())
return $l
Upvotes: 2