如何选择唯一节点 [英] How to select unique nodes

查看:34
本文介绍了如何选择唯一节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 这个页面 描述了 Muenchian 方法,但我想我应用错误.

考虑这将返回一组年龄:

/doc/class/person/descriptive[(@name='age')]/value

<块引用>

1..2..2..2..3..3..4..7

但我想要一个节点集,每个年龄只有一个节点.

<块引用>

1..2..3..4..7

这些似乎都返回所有值,而不是唯一值:

/doc/class/person/descriptive[(@name='age')][not(value=preceding-sibling::value)]/value/doc/class/person/descriptive[(@name='age')]/value[not(value=preceding-sibling::value)]

我错过了什么?

解决方案

举个例子:

<item type='test'>A</item><item type='test'>B</item><item type='test'>C</item><item type='test'>A</item><item type='other'>A</item><item type='test'>B</item><item type='other'>D</item><item type=''>A</item></root>

还有 XPath:

//preceding::item/preceding::item[not(.=preceding-sibling::item)]/text()

结果:A B C D

编辑:正如 mousio 评论的那样,如果它是唯一出现的时间,则它不会捕获列表中的最后一个项目.考虑到这一点和 Fëanor 的评论,这里有一个更好的解决方案:

/root/item[not(.=preceding-sibling::item)]

I found this page describing the Muenchian method, but I think I'm applying it wrong.

Consider that this would return a set of ages:

/doc/class/person/descriptive[(@name='age')]/value

1..2..2..2..3..3..4..7

But I would like a nodeset only one node for each age.

1..2..3..4..7

Each of these seem to return all of the values, instead of unique values:

/doc/class/person/descriptive[(@name='age')][not(value=preceding-sibling::value)]/value
/doc/class/person/descriptive[(@name='age')]/value[not(value=preceding-sibling::value)]

What am I missing?

解决方案

Here's an example:

<root>
    <item type='test'>A</item>
    <item type='test'>B</item>
    <item type='test'>C</item>
    <item type='test'>A</item>
    <item type='other'>A</item>
    <item type='test'>B</item>
    <item type='other'>D</item>
    <item type=''>A</item>
</root>

And the XPath:

//preceding::item/preceding::item[not(.=preceding-sibling::item)]/text()

Results: A B C D

EDIT: As mousio commented this doesn't capture the last item in a list if it's the only time it appears. Taking that and Fëanor's comment into account, here's a better solution:

/root/item[not(.=preceding-sibling::item)]

这篇关于如何选择唯一节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆