assuming the followin开发者_如何学Cg 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?
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.
精彩评论