xQuery 本地名称与带有 html 的 xPath
假设以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
index.html
是 XHTML,并且您要查找的位于 XHTML 命名空间中。
第一个查询忽略命名空间,因为您使用了
local-name()
函数。第二个查询则不然,它显式请求位于空命名空间中的
。
您需要
注意,我避免使用
//
,因为它会遍历文档的整个树,即使在这种情况下的唯一可能位置> 是预先知道的。使其显式可以大大加快 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
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.