XQuery "except" expression is not producing a desired result

61 views Asked by At

I am using BaseX latest version 11.0 beta.

I am trying to remove Texas cities in the /root/base/r sequence by using /root/remove/r sequence.

The goal is to use XQuery except expression.

Here is my XML and XQuery

declare context item := document {
<root>
  <base>
    <r>Miami</r>
    <r>Orlando</r>
    <r>Dallas</r>
    <r>Austin</r>
  </base>
  <remove>
    <r>Dallas</r>
    <r>Austin</r>
  </remove>
</root>
};

let $remove := /root/remove/r
return /root/base/r except $remove

Desired output

<r>Miami</r>
<r>Orlando</r>

Unfortunately, I am getting the same initial XML

<r>Miami</r>
<r>Orlando</r>
<r>Dallas</r>
<r>Austin</r>

It is not clear what I am doing wrong.

2

There are 2 answers

0
Christian Grün On BEST ANSWER

With except, the node references are compared, not their contents. For example, the following query will exclude nodes with the same node identity from the result:

let $remove := /root/base/r[position() > 2]
return /root/base/r except $remove

If you want to exclude all nodes of the base branch that have the string values of the nodes of the $remove branch, you can do this:

let $remove := /root/remove/r
return /root/base/r[not(. = $remove)]
0
Michael Kay On

The except operator compares nodes by node identity, not by string value.

You want

/root/base/r[not(. = /root/base/remove)]