选择包含“foo"的属性的正确 XPath 是什么? [英] What is the correct XPath for choosing attributes that contain "foo"?

查看:35
本文介绍了选择包含“foo"的属性的正确 XPath 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这个 XML,XPath 返回 prop 属性包含 Foo(前三个节点)的所有元素:

Given this XML, what XPath returns all elements whose prop attribute contains Foo (the first three nodes):

<bla>
 <a prop="Foo1"/>
 <a prop="Foo2"/>
 <a prop="3Foo"/>
 <a prop="Bar"/>
</bla>

推荐答案

//a[contains(@prop,'Foo')]

如果我使用此 XML 来获取结果,则有效.

Works if I use this XML to get results back.

<bla>
 <a prop="Foo1">a</a>
 <a prop="Foo2">b</a>
 <a prop="3Foo">c</a>
 <a prop="Bar">a</a>
</bla>

另一件要注意的事情是,虽然上面的 XPath 将返回该特定 xml 的正确答案,但如果您想保证只获得元素bla"中的a"元素,您应该像其他人提到的那样也使用

Another thing to note is that while the XPath above will return the correct answer for that particular xml, if you want to guarantee you only get the "a" elements in element "bla", you should as others have mentioned also use

/bla/a[contains(@prop,'Foo')]

这将搜索整个 xml 文档中的所有a"元素,无论是否嵌套在blah"元素中

This will search you all "a" elements in your entire xml document, regardless of being nested in a "blah" element

//a[contains(@prop,'Foo')]  

为了彻底和本着 stackoverflow 的精神,我添加了这个.:)

I added this for the sake of thoroughness and in the spirit of stackoverflow. :)

这篇关于选择包含“foo"的属性的正确 XPath 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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