祖先属性的Xpath测试不等于字符串 [英] Xpath test for ancestor attribute not equal string

查看:82
本文介绍了祖先属性的Xpath测试不等于字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试元素祖先上的属性是否不等于字符串.

I'm trying to test if an attribute on an ancestor of an element not equal a string.

这是我的XML ...

Here is my XML...

<aaa att="xyz">
<bbb>
<ccc/>
</bbb>
</aaa>
<aaa att="mno">
<bbb>
<ccc/>
</bbb>
</aaa>

如果我要对元素ccc进行操作,则尝试测试其祖父母aaa @att不等于"xyz".

If I'm acting on element ccc, I'm trying to test that its grandparent aaa @att doesn't equal "xyz".

我目前有这个...

ancestor::aaa[not(contains(@att, 'xyz'))]

谢谢!

推荐答案

假设通过说出元素的祖先,您所指的是具有子元素的元素,那么该XPath表达式应该执行以下操作:

Assuming that by saying an ancestor of an element you're referring to an element with child elements, this XPath expression should do:

//*[*/ccc][@att != 'xyz']

它选择

  1. 所有节点
  2. 具有至少一个< ccc> 孙子节点
  3. 并且具有 att 属性,其值不是 xyz .
  1. all nodes
  2. that have at least one <ccc> grandchild node
  3. and that have an att attribute whose value is not xyz.

更新:仅对< ccc> 的祖父母进行测试.

Update: Restricted test to grandparents of <ccc>.

更新2:适应您的修订问题:

//ccc[../parent::aaa/@att != 'xyz']

选择

  1. 所有< ccc> 元素
  2. 具有祖父母< aaa> 且其属性 att 设置为非 xyz
  3. 值的
  1. all <ccc> elements
  2. that have a grandparent <aaa> with its attribute att set to a value that is not xyz

这篇关于祖先属性的Xpath测试不等于字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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