加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 如何选择唯一节点

发布时间:2020-12-16 07:53:59 所属栏目:百科 来源:网络整理
导读:我发现 this page描述了Muenchian方法,但我认为我应用错了. 考虑到这将返回一组年龄: /doc/class/person/descriptive[(@name='age')]/value 1..2..2..2..3..3..4..7 但是我想要一个节点组,每个年龄只有一个节点. 1..2..3..4..7 每个都似乎返回所有的值,而不
我发现 this page描述了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)]

我失踪了什么

以下是一个例子:
<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>

而XPath:

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

结果:
A B C D

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读