xml 选择包含“foo”的属性的正确 XPath 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/103325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:03:23  来源:igfitidea点击:

What is the correct XPath for choosing attributes that contain "foo"?

xmlxpath

提问by ripper234

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

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

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

回答by evilhomer

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

Works if I use this XML to get results back.

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

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

Edit: 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

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

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

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

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

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

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

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

回答by Alex Beynenson

This XPath will give you all nodes that have attributes containing 'Foo' regardless of node name or attribute name:

无论节点名称或属性名称如何,此 XPath 都会为您提供属性包含“Foo”的所有节点:

//attribute::*[contains(., 'Foo')]/..

Of course, if you're more interested in the contents of the attribute themselves, and not necessarily their parent node, just drop the /..

当然,如果您对属性本身的内容更感兴趣,而不一定是它们的父节点,只需删除 /..

//attribute::*[contains(., 'Foo')]

回答by 1729

descendant-or-self::*[contains(@prop,'Foo')]

Or:

或者:

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

Or:

或者:

/bla/a[position() <= 3]

Dissected:

解剖:

descendant-or-self::

The Axis - search through every node underneath and the node itself. It is often better to say this than //. I have encountered some implementations where // means anywhere (decendant or self of the root node). The other use the default axis.

轴 - 搜索下面的每个节点和节点本身。通常这样说比 // 更好。我遇到了一些实现,其中 // 表示任何地方(根节点的后代或自身)。另一个使用默认轴。

* or /bla/a

The Tag - a wildcard match, and /bla/a is an absolute path.

标签 - 通配符匹配,/bla/a 是绝对路径。

[contains(@prop,'Foo')] or [position() <= 3]

The condition within [ ]. @prop is shorthand for attribute::prop, as attribute is another search axis. Alternatively you can select the first 3 by using the position() function.

[ ] 内的条件。@prop 是attribute::prop 的简写,因为attribute 是另一个搜索轴。或者,您可以使用 position() 函数选择前 3 个。

回答by Metro Smurf

John C is the closest, but XPath is case sensitive, so the correct XPath would be:

John C 是最接近的,但 XPath 区分大小写,所以正确的 XPath 应该是:

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

回答by SomeDudeSomewhere

If you also need to match the content of the link itself, use text():

如果您还需要匹配链接本身的内容,请使用 text():

//a[contains(@href,"/some_link")][text()="Click here"]

//a[contains(@href,"/some_link")][text()="Click here"]

回答by toddk

Have you tried something like:

您是否尝试过类似的事情:

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

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

I've never used the contains function before but suspect that it should work as advertised...

我以前从未使用过 contains 函数,但怀疑它应该像宣传的那样工作......

回答by David Hill

/bla/a[contains(@prop, "foo")]

/bla/a[contains(@prop, "foo")]

回答by digiguru

For the code above... //*[contains(@prop,'foo')]

对于上面的代码... //*[contains(@prop,'foo')]

回答by Dani Duran

try this:

尝试这个:

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

//a[包含(@prop,'foo')]

that should work for any "a" tags in the document

这应该适用于文档中的任何“a”标签