Jaxen /text() 不起作用
我有 html:
<p>
<a href="#">click here</a>
Welcome
</p>
我只想使用 Xpath 结合 Jaxen lib 检索“欢迎”部分我正在使用的 Xpath 是;
//p/text()
现在,当我删除 /text() 时,它会检索;
click here
Welcome
添加 /text() 后,它会检索 null 有没有其他方法可以检索 p 标签内的所有内容但排除任何其他标签?
I have the html:
<p>
<a href="#">click here</a>
Welcome
</p>
And I just want to retrieve the "Welcome" part using Xpath combined with the Jaxen lib the Xpath I am using is;
//p/text()
Now when I remove the /text() it retrieves;
click here
Welcome
With the /text() added it retrieve null
Is there any other way to retrieve everything inside the p tag but excluding any other tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 XML 解析器的角度来看,有多个文本元素可供选择(Welcome 及其前后的空格),因此它不会选择任何一个。您有几个选项,主要是在解析之前去除空格或者更具体地了解查询,例如选择第二个最文本元素:
From the XML parser point of view, there are multiple text elements to choose from (Welcome and the whitespace preceding and following it), so it doesn't choose any one. You have a few options, mainly stripping the whitespace before parsing or being more specific about the query, like selecting the second most text element: