Xpath 获取具有与另一个节点匹配的属性的所有节点 [英] Xpath Getting All Nodes that Have an Attribute that Matches Another Node

查看:34
本文介绍了Xpath 获取具有与另一个节点匹配的属性的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到具有与其他项目相同的值的属性的所有节点.我有应该用于查找的项目和应该比较的属性.这可能吗?

I want to find all nodes that have an attribute with a value that is the same as some other item. I have the item that should be used to do the lookup and the attribute that should be compared. Is this possible?

<base src="www.placeholder.com"></base>
<parent src="www.test.com">
   <child name="child1" src="www.test.com"></child>
   <child name="child2" src="www.placeholder.com"></child>
   <child name="child3" src="www.test.com"></child>
</parent>

我想编写一个 xpath 查询来获取与基本节点 (child2) 具有相同源的所有节点,但我无法将 www.placeholder.com 硬编码到我的 xpath 查询中.

I want to write an xpath query that gets all nodes that have the same source as the base node (child2) but I can't hard code www.placeholder.com into my xpath query.

推荐答案

假设整个 XML 片段都包含在一个根元素中,例如说 ,(否则它不是't 格式良好的 XML),然后您可以使用以下 XPath 表达式:

Assuming that the entire XML snippet is wrapped in a root element, say <root> for example, (otherwise it isn't well-formed XML), you can then use the following XPath expression :

//*[@src = /root/base/@src]

结果是所有元素,其中src属性等于base元素的src,包括base元素本身:

The result is all elements, where src attribute equals base element's src, including the base element itself :

<base src="www.placeholder.com"/>

<child name="child2" src="www.placeholder.com"/>

可以在此处

这篇关于Xpath 获取具有与另一个节点匹配的属性的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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