用于返回合格子节点值的字符串串联的 XPath
任何人都可以建议一种 XPath 表达式格式,该格式返回一个字符串值,其中包含元素的某些合格子节点的串联值,但忽略其他值:
<div>
This text node should be returned.
<em>And the value of this element.</em>
And this.
<p>But this paragraph element should be ignored.</p>
</div>
返回的值应该是单个字符串:
This text node should be returned. And the value of this element. And this.
这在单个 XPath 表达式中可能吗?
谢谢。
Can anyone please suggest an XPath expression format that returns a string value containing the concatenated values of certain qualifying child nodes of an element, but ignoring others:
<div>
This text node should be returned.
<em>And the value of this element.</em>
And this.
<p>But this paragraph element should be ignored.</p>
</div>
The returned value should be a single string:
This text node should be returned. And the value of this element. And this.
Is this possible in a single XPath expression?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 XPath 2.0 中:
string-join(/*/node()[not(self::p)], '')
In XPath 2.0 :
string-join(/*/node()[not(self::p)], '')
在 XPath 1.0 中:
您可以用来
捕获所需的文本节点。连接本身无法在 XPath 1.0 中完成,我建议在主机应用程序中完成。
In XPath 1.0:
You can use
to capture the wanted text nodes. The concatenation itself cannot be done in XPath 1.0, I recommend doing it in the host application.
无论中间节点如何,双斜杠都会强制提取文本
double slash forces to extract text regardless of intermediate nodes
这种外观有效:
用作上下文
/div/
:或者不使用上下文:
如果要连接前两个字符串,请使用以下内容:
This look that works:
Using as context
/div/
:Or without the use of context:
If you want to concat the first two strings, use this:
如果您想要除 p 之外的所有子级,您可以尝试以下...
返回...
If you want all children except p, you can try the following...
which returns...
我知道这有点晚了,但我认为我的答案仍然有意义。我最近遇到了类似的问题。而且因为我在Python 3.6中使用scrapy,它不支持xpath 2.0,所以我无法使用几个在线答案中建议的string-join函数。
我最终找到了一个简单的解决方法(如下所示),我在任何 stackoverflow 答案中都没有看到该解决方法,这就是我分享它的原因。
希望这有帮助!
I know this comes a bit late, but I figure my answer could still be relevant. I recently ran into a similar problem. And because I use
scrapy
in Python 3.6, which does not support xpath 2.0, I could not use thestring-join
function suggested in several online answers.I ended up finding a simple workaround (as shown below) which I did not see in any of the stackoverflow answers, that's why I'm sharing it.
Hope this helps!
您也可以使用 for-each 循环并将值组合在变量中,如下所示
You could use a for-each loop as well and assemble the values in a variable like this