xml 如何使用 XPath 2.0 识别数字序列中的重复值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/133092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:05:30  来源:igfitidea点击:

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

xmlxpathxpath-2.0

提问by Dimitre Novatchev

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

我有一个 XPath 表达式,它为我提供了一系列如下所示的值:

1 2 2 3 4 5 5 6 7

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 7using 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?

这是很容易将其转换为一组唯一值的1 2 3 4 5 6 7使用distinct-values()。但是,我要提取的是重复值列表 = 2 5。我想不出一个简单的方法来做到这一点。任何人都可以帮忙吗?

回答by Dimitre Novatchev

Use this simple XPath 2.0 expression:

使用这个简单的 XPath 2.0 表达式

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

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

哪里$vSeq是我们要在其中找到重复项的值序列。

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/

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.

TLDR;这张图片可以是一个直观的解释。

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

然后评估上面的 XPath 表达式产生: 2, 5, 7



enter image description here

在此处输入图片说明

回答by JeniT

What about:

关于什么:

distinct-values(
  for $item in $seq
  return if (count($seq[. eq $item]) > 1)
         then $item
         else ())

This iterates through the items in the sequence, and returns the item if the number of items in the sequence that are equal to that item is greater than one. You then have to use distinct-values()to remove the duplicates from that list.

这将遍历序列中的项目,如果序列中等于该项目的项目数大于 1,则返回该项目。然后您必须使用distinct-values()从该列表中删除重复项。

回答by GerG

Calculate the difference between your original set and the set of distinct values. This is the set of numbers that occur more than once. Note that numbers in this result set are not necessarily distinct if they occur more than twice in the original sequence so convert again to a set of distinct values if this is required.

计算原始值集和不同值集之间的差异。这是出现不止一次的一组数字。请注意,如果此结果集中的数字在原始序列中出现两次以上,则它们不一定是不同的,因此如果需要,请再次转换为一组不同的值。

回答by michal kralik

What about xsl? Is it applicable to your request?

xsl呢?它适用于您的要求吗?

    <xsl:for-each select="/r/a">
        <xsl:variable name="cur" select="." />
        <xsl:if test="count(./preceding-sibling::a[. = $cur]) > 0 and count(./following-sibling::a[. = $cur]) = 0">
            <xsl:value-of select="." />
        </xsl:if>
    </xsl:for-each>