xQuery 本地名称与带有 html 的 xPath

发布于 2024-08-25 04:00:34 字数 445 浏览 2 评论 0原文

假设以下 html (减去 xQuery 不会处理的注释和“nbsp;”等)是)为什么下面的代码可以工作

for $first in fn:doc("file:///index.html")//element()[local-name() = "head"]
    return <test>{ $first }</test>

而这个

for $first in fn:doc("file:///index.html")//head
return 
<test>{ $first }</test>

不行?

assuming the following html (minus the comments and "nbsp;" etc that xQuery wont process as is) why does this following code work

for $first in fn:doc("file:///index.html")//element()[local-name() = "head"]
    return <test>{ $first }</test>

and this

for $first in fn:doc("file:///index.html")//head
return 
<test>{ $first }</test>

not work?

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

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

发布评论

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

评论(1

季末如歌 2024-09-01 04:00:34

因为 index.html 是 XHTML,并且您要查找的 位于 XHTML 命名空间中。

第一个查询忽略命名空间,因为您使用了 local-name() 函数。

第二个查询则不然,它显式请求位于空命名空间中的

您需要

declare namespace x="http://www.w3.org/1999/xhtml"

for $first in fn:doc("file:///index.html")/x:html/x:head
return <test>{ $first }</test>

注意,我避免使用 //,因为它会遍历文档的整个树,即使在这种情况下 的唯一可能位置> 是预先知道的。使其显式可以大大加快 XPath 查询的速度。

Because index.html is XHTML and the <head> you are looking for is in the XHTML namespace.

The first query ignores namespaces because you use the local-name() function.

The second query does not, it explicitly asks for a <head> that is in the empty namespace.

You would need

declare namespace x="http://www.w3.org/1999/xhtml"

for $first in fn:doc("file:///index.html")/x:html/x:head
return <test>{ $first }</test>

Note that I avoid using //, since this goes through the entire tree of the document, even though in this case the only possible position of the <head> is known beforehand. Making it explicit speeds up the XPath query a lot.

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