XPath 使用 Xpath 2.0 选择具有特定属性的元素的唯一列表 [英] XPath to select unique list of elements that have a certain attribute with Xpath 2.0

查看:26
本文介绍了XPath 使用 Xpath 2.0 选择具有特定属性的元素的唯一列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取具有特定属性的唯一元素列表(基于名称)时遇到了一些麻烦.我可以得到其中任何一个的列表,但组合列表是什么让我绊倒.我的 xml 看起来像这样:

I'm having a bit of trouble getting a unique list of elements (based on name) that also have a certain attribute. I can get a list of either of those, but the combined list is what's tripping me up. My xml looks like this:

<global>
    <discipline name="tracks">
        <group name="Normal"/>
        <group name="Gloss" PackGroup="Diffuse"/>
        <group name="Mask"/>
        <group name="Diffuse"/>
        <group name="Tint" PackGroup="Gloss"/>
    </discipline>
    <discipline name="trains">
        <group name="Mask"/>
        <group name="Diffuse" PackGroup="Mask"/>
    </discipline>
</global>

使用这个 xpath 查询,我可以很容易地得到一个不同的组元素列表:

i can get a distinct list of group elements easily enough using this xpath query:

/configuration/global/discipline/group[not(@name = following::group/@name)]

获取定义了 PackGroup 属性的组元素列表也很简单:

and getting the list of group elements that have PackGroup attribute defined is trivial as well:

/configuration/global/discipline/group[@PackGroup]

但是,我似乎无法获得定义了 PackGroup 属性的元素的唯一列表.我试过这个:

however, i can't seem to get a unique list of elements that have PackGroup attribute defined. i've tried this:

/configuration/global/discipline/group[not(@name = following::group/@name) and @PackGroup]
/configuration/global/discipline/group[not(@name = following::group/@name) and contains(@PackGroup, '*')]

不返回任何元素和

/configuration/global/discipline/group[not(@name = following::group/@name) and contains(@PackGroup, '')]

只返回唯一列表.有谁知道正确的 xpath 查询是什么才能得到我要找的东西?

which returns just the unique list. Does anyone know what the correct xpath query is to get what i'm looking for?

(我正在使用 C# 和 xpath 2.0,以防万一)

(i'm working in C# and xpath 2.0 in case that matters)

谢谢!

推荐答案

我想你要找的是

/configuration/global/discipline/group
  [not(@name = following::group[@Packgroup]/@name) and @PackGroup]

您要确保您正在过滤的组(表达式中的第一个 group)和您正在检查的下一个组(第二个 group代码>) 有一个@Packgroup 属性.

You want to make sure that both the group you're filtering (the first group in the expression) and the following group that you're checking for (the second group) have a @Packgroup attribute.

也有一种方法可以使用 distinct-values() 函数,但我目前还没有想过如何.

There may also be a way to do this using the distinct-values() function, but I haven't thought how at this point.

这篇关于XPath 使用 Xpath 2.0 选择具有特定属性的元素的唯一列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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