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?
Because
index.htmlis 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.