Raphael St
Raphael St

Reputation: 671

Xquery, use deep-equal with extra elements

I used this function to check if a sequence do exist in another sequence : http://www.xqueryfunctions.com/xq/fn_deep-equal.html

fn:deep-equal()

The problem is that when it compares two sequences, if one sequence possess all the element of the others plus her own ones, it returns false!

I 'd like to know how it could return true if there is "at least the minimum sequence" defined by one of the function attribute.

Cheers

Upvotes: 0

Views: 220

Answers (2)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

No, deep-equal checks for structural isomorphism and identity of values. You have two options:

1 Write your own recursive function contains_subseq($a, $b) to determine whether $a contains $b as a sub-sequence (you may be able to use deep-equal on the members of the two sequences, depending on what you actually are looking for).

If the requirement is not just that all items in $a appear as items in $b, in the same order, but also that they be contiguous and begin at position 1 (i.e. if the requirement is that sequence $a appear as a prefix of sequence $b), then the solution proposed by Michael Kay will work and you don't need a function of your own. If (1, 3, 5) and (1, 2, 3, 4, 5) should match, or if (2, 3, 4) and (1, 2, 3, 4, 5) should match, however, then you're going to need more than that.

2 If you know in advance what the required minimum sequence is, you can write a function to normalize a sequence by discarding everything else, and then use deep-equals to test whether your normalized sequence matches the minimum.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163322

Try:

let $len := min((count($a), count($b)))
return deep-equal(subsequence($a, 1, $len), subsequence($b, 1, $len)) 

This is assuming the length difference applies only to the top-level sequence, and not to sequences of children deeper down the tree.

Upvotes: 1

Related Questions