xml 如何在此处使用 XPath contains()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1064968/
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:35:04  来源:igfitidea点击:

How to use XPath contains() here?

xmlxpath

提问by ryeguy

I'm trying to learn XPath. I looked at the other contains()examples around here, but nothing that uses an ANDoperator. I can't get this to work:

我正在尝试学习 XPath。我查看了contains()这里的其他示例,但没有使用AND运算符。我无法让它工作:

//ul[@class='featureList' and contains(li, 'Model')]

On:

在:

...
<ul class="featureList">

<li><b>Type:</b> Clip Fan</li><li><b>Feature:</b> Air Moved: 65 ft.
    Amps: 1.1
    Clip: Grips any surface up to 1.63"
    Plug: 3 prong grounded plug on heavy duty model
    Usage: Garage, Workshop, Dorm, Work-out room, Deck, Office & more.</li><li><b>Speed Setting:</b> 2 speeds</li><li><b>Color:</b> Black</li><li><b>Power Consumption:</b> 62 W</li><li><b>Height:</b> 14.5"</li><li><b>Width:</b> Grill Diameter: 9.5"</li><li><b>Length:</b> 11.5"</li>

<li><b>Model #: </b>CR1-0081-06</li>
<li><b>Item #: </b>N82E16896817007</li>
<li><b>Return Policy: </b></li>
</ul>
...

回答by Jeff Yates

You are only looking at the first lichild in the query you have instead of looking for any lichild element that may contain the text, 'Model'. What you need is a query like the following:

您只查看li查询中的第一li个子元素,而不是查找任何可能包含文本'Model'. 您需要的是如下查询:

//ul[@class='featureList' and ./li[contains(.,'Model')]]

This query will give you the elements that have a classof featureListwith one or more lichildren that contain the text, 'Model'.

此查询将为您class提供featureList具有一个或多个li包含文本'Model'.

回答by Tomalak

I already gave my +1 to Jeff Yates' solution.

我已经为 Jeff Yates 的解决方案提供了 +1。

Here is a quick explanation why your approach does not work. This:

这是为什么您的方法不起作用的快速解释。这个:

//ul[@class='featureList' and contains(li, 'Model')]

encounters a limitation of the contains()function (or any other string function in XPath, for that matter).

遇到contains()函数(或 XPath 中的任何其他字符串函数,就此而言)的限制。

The first argument is supposed to be a string. If you feed it a node list (giving it "li" does that), a conversion to string must take place. But this conversion is done for the first node in the list only.

第一个参数应该是一个字符串。如果你给它一个节点列表(给它“ li”),就必须转换为字符串。但是这种转换只对列表中的第一个节点进行。

In your case the first node in the list is <li><b>Type:</b> Clip Fan</li>(converted to a string: "Type: Clip Fan") which means that this:

在您的情况下,列表中的第一个节点是<li><b>Type:</b> Clip Fan</li>(转换为字符串:“ Type: Clip Fan”),这意味着:

//ul[@class='featureList' and contains(li, 'Type')]

would actually select a node!

实际上会选择一个节点!

回答by kjhughes

This is a new answer to an old question about a common misconceptionabout contains()in XPath...

这是一个关于XPath 中常见误解的旧问题的新答案contains()......

Summary: contains()means contains a substring, notcontains a node.

总结:contains()意味着包含一个子字符串包含一个节点

Detailed Explanation

详细说明

This XPath is often misinterpreted:

这个 XPath 经常被误解:

//ul[contains(li, 'Model')]

Wrong interpretation:Select those ulelements that containan lielement with Modelin it.

错误的解释:选择那些ul的元素包含li元素与Model它。

This is wrong because

这是错误的,因为

  1. contains(x,y)expects xto be a string, and
  2. the XPath rule for converting multiple elements to a string is this:

    A node-set is converted to a string by returning the string-valueof the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.

  1. contains(x,y)期望x是一个字符串,并且
  2. 将多个元素转换为字符串的 XPath 规则是这样的

    通过返回节点集中在文档顺序中排在第一位的节点的字符串值,将节点集转换为字符串。如果节点集为空,则返回空字符串。

Right interpretation:Select those ulelements whose firstlichild has a string-valuethat containsa Modelsubstring.

正确的解释:选择那些ul其元素第一li的孩子有一个字符串值,包含一个Model字符串。

Examples

例子

XML

XML

<r>
  <ul id="one">
    <li>Model A</li>
    <li>Foo</li>
  </ul>
  <ul id="two">
    <li>Foo</li>
    <li>Model A</li>
  </ul>
</r> 

XPaths

XPaths

  • //ul[contains(li, 'Model')]selects the oneulelement.

    Note:The twoulelement is not selected because the string-value of the first lichild of the twoulis Foo, which does not contain the Modelsubstring.

  • //ul[li[contains(.,'Model')]]selects the oneand twoulelements.

    Note:Both ulelements are selected because contains()is applied to each liindividually. (Thus, the tricky multiple-element-to-string conversion rule is avoided.) Both ulelements do have an lichild whose string value contains the Modelsubstring -- position of the lielement no longer matters.

  • //ul[contains(li, 'Model')]选择oneul元素。

    注意:twoul元素未被选中,因为 的第一li个子元素的字符串值twoulFoo,其中不包含Model子字符串。

  • //ul[li[contains(.,'Model')]]选择onetwoul元素。

    注意:这两个ul元素都被选中,因为contains()它们li分别应用于每个元素。(因此,避免了棘手的多元素到字符串转换规则。)这两个ul元素确实有一个li子元素,其字符串值包含Model子字符串——li元素的位置不再重要。

See also

也可以看看

回答by runrig

//ul[@class="featureList" and li//text()[contains(., "Model")]]

回答by hahakubile

Paste my containsexample here:

contains在这里粘贴我的例子:

//table[contains(@class, "EC_result")]/tbody