How do you identify duplicate values in a numerical sequence using XPath 2.0?

woody picture woody · Sep 25, 2008 · Viewed 29.5k times · Source

I have an XPath expression which provides me a sequence of values like the one below:

1 2 2 3 4 5 5 6 7

It is easy to convert this to a set of unique values 1 2 3 4 5 6 7 using distinct-values() . However, what I want to extract is the list of duplicate values = 2 5. I can't think of an easy way to do this. Can anyone help?

Answer

Dimitre Novatchev picture Dimitre Novatchev · Nov 13, 2008

Use this simple XPath 2.0 expression:

$vSeq[index-of($vSeq,.)[2]]

where $vSeq is the sequence of values in which we want to find the duplicates.

For explanation of how this "works", see:

http://dnovatchev.wordpress.com/2008/11/16/xpath-2-0-gems-find-all-duplicate-values-in-a-sequence-part-2/

TLDR; This picture can be a visual explanation.

If the sequence is:

$vSeq  =  1,   2,   3,   2,   4,   5,   6,   7,   5,   7,   5

Then evaluating the above XPath expression produces: 2, 5, 7


enter image description here