Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...