The following two xPaths should be the exact same:
/theNameOfMyElement
and
/*[fn:name()="theNameOfMyElement"]
The latter is adding an unnecessary and costly qualifier. First off, the *
has to search for everything, not just elements. Several other problems exist with that approach.
If my first query is still taking a long time, use cts:search
, which is much faster as it searches against indexes. The queries above can be written like this:
cts:search(/theNameOfMyElement, ())
Where the second parameter (empty sequence) can be a qualifying cts:query
.
If namespaces are giving you fits, you can just do:
/*:theNameOfMyElement
/*[fn:name()="something"]
seems like very bad practice to me. Use /something
instead.
EDIT
After seeing that the other answer got accepted, I've been trying to think of what scenario you must be trying to solve if his solution worked and mine didn't. I'm still very certain that there is a faster way by just using xPath the way it was designed to work.
After some thought, I've decided your "real" scenario must either involve a dynamic element name or you may be trying to see if the element name matches one of a sequence of names.
I have drawn up a sample with it's output provided below, that demonstrates how you could still use both without using the qualifier based on fn:node-name
let $xml as element(sample) := <sample>
<wrapper>
<product>
<entry>
<red>$1.00</red>
<yellow>$3.00</yellow>
<blue>$4.50</blue>
</entry>
</product>
</wrapper>
</sample>
let $type as xs:string := "product"
return $xml/wrapper/xdmp:unpath($type)/entry/(red|yellow)
(: Returns
<red>$1.00</red>
<yellow>$3.00</yellow>
:)