在 xpath 中使用 YQL 和 substring-before

发布于 2024-11-18 03:44:35 字数 360 浏览 7 评论 0原文

我正在尝试使用 xpath 在 html 页面中的一个段落中获取“--”之前的字符串并将其发送到 yql,

例如我想从以下文章中获取日期:

<div>
<p>Date --- the body of the article</p>
</div>

我在 yql 中尝试了此查询:

select * from html where url="article url" and xpath="//div/p/text()/[substring-before(.,'--')]"

但确实如此不工作。

如何获取“--”之前的文章日期

I am trying to get a string before '--' within a paragraph in an html page using the xpath and send it to yql

for example i want to get the date from the following article:

<div>
<p>Date --- the body of the article</p>
</div>

I tried this query in yql:

select * from html where url="article url" and xpath="//div/p/text()/[substring-before(.,'--')]"

but it does not work.

how can I get the date of the article which is before the '--'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小ぇ时光︴ 2024-11-25 03:44:35

您可以简单地使用:

  substring-before(//div/p,'--')

You can simply use:

  substring-before(//div/p,'--')
岛歌少女 2024-11-25 03:44:35

使用

substring-before(/div/p/text(), '--')

此 XPath 表达式的计算结果为紧邻 XML 文档中第一个文本节点中 '--' 之前的字符串,该节点是 p< 的子节点。 /code> 是 div 顶部元素的子元素。

如果您想要为每个此类文本节点获取此值,则必须使用如下表达式:

substring-before((//div/p/text())[$k], '--')

并计算此表达式 $N 次,对于 $k = 1,2, ..., $N

其中 $Ncount(//div/p/text())

请注意:尽量避免使用 // XPath 伪运算符总是当 XML 文档的结构静态已知时。使用 // 通常会导致效率低下 (O(N^2)),这对于大型 XML 文档来说尤其痛苦。

Use:

substring-before(/div/p/text(), '--')

This XPath expression evaluates to the string immediately preceding '--' in the first text node in the XML document, that is a child of a p that is a child of the div top element.

In case you want to get this value for every such text node, you have to use an expression like:

substring-before((//div/p/text())[$k], '--')

and evaluate this expression $N times, for $k = 1,2, ..., $N

where $N is count(//div/p/text())

Do note: Try to avoid using the // XPath pseudo-operator always when the structure of the XML document is statically known. Using // usually results in big inefficiency (O(N^2)) that are felt especially painful on big XML documents.

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