XQuery/Compare with XQuery

Motivation
You want to use XQuery to compare two lists of items and find out how the lists are different

Methods
We will use a variety of functions that iterate through the lists. For each example we will perform some comparison with the second list.

Simple iteration and test for missing elements
In this first example, we will use a simple for loop to go through each item on a linear list. We then will check to see if that item is anywhere on the second list (regardless of order). If it is on the second list we will display the item from the first list. If not we will output "missing". This is useful if you want to find out if a local collection is missing files from some remote collection. Note that the conditional expression:

if ($list2/item/text = $item-text)

Tests to see if the $item-text is anywhere in list2. If it occurs anywhere this expression will return true.

Sample Results
Note that this will not report any items on the second list that are missing from the first list.

Using Quantified Expressions
This can be rewritten using XQuery quantified expressions. There are two reasons for this. First the XQuery optimizer can frequently run quantified expressions much faster and some people feel they are easier to read. See XQuery/Quantified Expressions for more details.

In this second example the list assignments are the same but we will only display the items from list 1 that are missing from list 2.

This returns:

We are now ready to modularize this missing function so that we can pass any two lists to find missing elements.

Creating a Missing XQuery Function
Our next step is to create an XQuery function that compare any two lists and returns the items in the second list that are not in the first list.

We can rewrite the output function to use this function:

Note that the order of the lists has been reversed in the second call to the missing function. The second pass looks for items on list2 that are not on list1.

Running this query generates the following output:

Creating HTML Difference Lists
We can use CSS to style the output of these reports.

Sample Data
This example uses full words of items to show text highlighting:

The following function uses HTML div and span elements and adds class="missing" to each div that is missing. The CSS file will highlight this background.

We then use the following CSS file to highlight the differences. Each missing element must have class="missing" attribute for the missing element to be highlighted in this report.

Collation
If the lists are in sorted order, or can be sorted into order, an alternative approach is to recursively collate the two lists. The core algorithm looks like:

With the example above, we can merge two lists.

Execute

The actions on merge will depend on the application and the algorithm can be modified to output only mismatched items on one or other list, and handle matching items appropriately. For example, to display the merged list as HTML, we might modify the algorithm to:

Execute