如何使用 XPath contains() [英] How to use XPath contains()
问题描述
我的 XML
<r>
<ul>
<li>ModelA</li>
<bi>Foo</bi>
</ul>
<ul>
<li>ModelB</li>
<bi>boo</bi>
</ul>
</r>
对于特定的节点值,我想提取后继节点的值.
For specific node value I want to extract the value of the successor node.
li = ModelA 得到 bi 的值为 Foo 的例子
Example when li = ModelA get the value of bi which is Foo
我的 Xpath : //ul[li[contains(.,'ModelA')]]
My Xpath : //ul[li[contains(.,'ModelA')]]
推荐答案
如果我正确理解您的问题,这可能会解决问题:
If I understand your question correctly, this may do the trick:
//ul/li[contains(.,'ModelA')]/following-sibling::bi
遵循@Thomas Weller 在下面评论中的正确观察,这里是对这个 xpath 表达式的简单解释:
Following @Thomas Weller's correct observation in the comments below, here's a simple explanation of this xpath expression:
你的目标
是目标
的子节点,它本身有另一个子节点 (,因此,
//ul/li
部分),它本身(即 )包含文本
li[contains(.,'模型A')]
).由于 和
作为同一个
是兄弟,一旦 的孩子,
<li>
定位,下一站是定位它的兄弟
(因此最终的/following-sibling::bi
).
Your target <bi>
is a child of the target <ul>
which itself has another child (<li>
, hence, the //ul/li
part), which itself (i.e., the <li>
) contains the text li[contains(.,'ModelA')]
). Since both <li>
and <bi>
, as children of the same <ul>,
are siblings, once that <li>
is located, the next stop is to locate its sibling <bi>
(hence the final /following-sibling::bi
).
这篇关于如何使用 XPath contains()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!