开发者

Getting the functionality of the MINUS SQL operator in XQuery

开发者 https://www.devze.com 2023-03-13 22:52 出处:网络
From previous queries, I have two variables. The first: <list> <item>a</item> <item>b</item>

From previous queries, I have two variables. The first:

<list>
    <item>a</item>
    <item>b</item>
    <item>a</item>
    开发者_如何学C<item>c</item>
</list>

The second:

<list>
    <item>b</item>
</list>

I want the nodes which are in the first list, but not in the second list:

<list>
    <item>a</item>
    <item>c</item>
</list>

(like the SQL MINUS operator) How can I do that?

EDIT: Fortunately, WikiBooks already has the answer.


Try the following, which is based on a fundamental of XPath2.0 - that the = operator acts as a kind of join, and allows implicit iteration (in this case, combined with ! for not).

Notice the variable $onlyin1 could be used directly to output the nodes from one list which don't exist in the second list, but the code below allows you to de-duplicate the nodes which have identical content as well.

let 
$list1 := 
<list>
    <item>a</item>
    <item>b</item>
    <item>a</item>
    <item>c</item>
</list>,
$list2 := 
<list>
    <item>b</item>
</list>,
$onlyin1 := $list1//item[text()!=$list2//item/text()]
return 
<list>
   {for $item in distinct-values($onlyin1/text()) return 
   <item>{$item}</item>
   }
</list>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号