XPath:有关 HTML 中的 p 元素的问题
我有一个关于 XPath 和 HTML 中的 p 元素的问题。假设我遇到一个如下所示的 HTML 结构:
<div id="this-is-a-text">
This is text segment 1.
<p>This is text segment 2.</p>
this is text segment 3.
<div id="this-is-not-part-of-the-text">This doesn't belong to the text.</div>
This is text segment 4.
</div>
我想知道解析所有文本段的正确方法是什么,无论它们是否位于 p 元素内? (注意:元素的顺序是随机的。)
我不明白为什么 //div[@id="this-is-a-text"]/p 似乎这样做作业(而不是仅返回文本段 3),而 //div[@id="this-is-a-text"]/text() 根本不返回任何结果。
谁能帮助我理解这一点?
谢谢!
鲍勃
I have a question concerning XPath and the p-element from HTML. Let's say I'm confronted with an HTML-structure that looks like this:
<div id="this-is-a-text">
This is text segment 1.
<p>This is text segment 2.</p>
this is text segment 3.
<div id="this-is-not-part-of-the-text">This doesn't belong to the text.</div>
This is text segment 4.
</div>
I'm wondering what's the correct way to parse all all text segments no matter if they're inside a p-element or not? (NB: The the sequence of the elements is random.)
What I don't understand is why //div[@id="this-is-a-text"]/p seems to do the job (instead of just returning text segment 3), whereas //div[@id="this-is-a-text"]/text() doesn't return any results at all.
Can anyone help me understand this?
Thanks!
Bob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Martin Honnen 提到的,查询
//div[@id="this-is-a-text"]/text()
应返回三个文本段的集合:如果我正确理解你的问题,你需要像这样的查询
这应该返回集合:
As Martin Honnen mentioned, query
//div[@id="this-is-a-text"]/text()
should return set of three text segments:If I understand your question right, you need query like
And this should return set: