[英]Xpath query to find nodes whose descendants contain particular text
Been lurking here for a long time, but I needed to ask this. 在这里潜伏了很长时间,但是我需要问这个。 Please excuse my noobity.
请原谅。
Doing some PHP/Xpath coding for some scraping, and I want to know an XPath expression to select nodes which have a parent whose sibling, somewhere in their descendant tree, contain a node with a particular text value. 进行一些PHP / Xpath编码以进行某些抓取,我想知道一个XPath表达式来选择具有父节点的节点,这些节点的父级在其后代树中的某个地方包含具有特定文本值的节点。
Say the node is something like span[@ng="league"] and the text value somewhere in a descendant is 'SKT', I believe it should in some way include contains(text(), 'SKT'), but I'm not quite sure on the rest. 假设节点类似于span [@ ng =“ league”],并且后代中某处的文本值为“ SKT”,我认为它应该以某种方式包含contains(text(),'SKT'),但我我不太确定其余的。 TIA.
TIA。
Edit: 编辑:
I've tried to create a diagram of the situation here 我试图在这里创建情况图
**
|
|
+[parent]
| |
| |
| [the node I want]
|
|
|
|
+[sibling of "parent" node seen above]
| |
| *
| |
| +---[specific text, found with previous xpath query]
|
etc**
If your xml is something as 如果您的xml是
<parent>
<span ng="league">The node you want </span>
</parent>
<any>
<any2>
<any3>SKT</any3>
</any2>
</any>
you can use such xpath 你可以使用这样的xpath
//span[@ng="league"][../following-sibling::*[contains(., "SKT")]]
The following XPath will return span[@ng="league"]
elements where there is at least one text node anywhere within the span
that contain substring 'SKT': 下面的XPath将返回
span[@ng="league"]
元素,其中存在中的至少一个文本节点的任何地方span
包含子串“SKT”:
//span[@ng="league" and .//text()[contains(., 'SKT')]]
If this doesn't work then you need to be more specific ie post minimal HTML/XML example (formatted text, not image) where the XPath above doesn't return the desired output 如果这不起作用,则需要更具体,即发布最小的HTML / XML示例(格式化的文本,而不是图像),其中上述XPath不能返回所需的输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.