XPath 查找最近的祖先元素,该元素包含具有特定值的属性的元素 [英] XPath to find nearest ancestor element that contains an element that has an attribute with a certain value

查看:21
本文介绍了XPath 查找最近的祖先元素,该元素包含具有特定值的属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ancestor::foo[bar[@attr="val"]]

我认为这会奏效,但事实并非如此.我需要在树中找到最近的 foo 元素,该元素具有 bar 子元素,而该子元素又具有 attr 属性 <代码>验证.

I thought this would work, but it's not. I need to find the nearest foo element going up in the tree that has a bar child element, which in turn has an attr attribute of val.

推荐答案

这应该有效:

//*[ancestor::foo[bar[@attr="val"]]]

或者替代

root/foo/bar[ancestor::foo[bar[@attr="val"]]]

匹配第二个 元素

<root>
    <foo>
        <bar attr="xxx"></bar>
    </foo>
    <foo>
        <bar attr="val"></bar>
    </foo>
    <foo>
        <bar attr="zzz"></bar>
    </foo>
</root>

这篇关于XPath 查找最近的祖先元素,该元素包含具有特定值的属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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