Jaxen /text() 不起作用

发布于 2024-08-17 09:25:08 字数 428 浏览 5 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-08-24 09:25:08

从 XML 解析器的角度来看,有多个文本元素可供选择(Welcome 及其前后的空格),因此它不会选择任何一个。您有几个选项,主要是在解析之前去除空格或者更具体地了解查询,例如选择第二个最文本元素:

//p/text()[2]

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:

//p/text()[2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文