XQuery: Optimizations on namespaced databases
In the database storage of BaseX, element and attribute names are indexed by their prefix:local representation, and the namespace is retrieved at runtime. This is why some compile-time query optimizations are skipped if a database has namespaces.
There are cases, though, in which a more fine-grained namespace detection would be possible and desirable. For example, if a document only has non-default namespace declarations…
<a xmlns:pre='URI'><b>B</b></a>
…and if an element/attribute name in an XPath step has no namespace (e.g. //b), it can safely be associated with elements of the accessed database. Steps with non-existing elements can then be replaced with empty sequences, or a text() step can be attached to a path expression that may later be rewritten for index access.
Code: https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/util/index/IndexInfo.java#L83
Script:
create db simple <a xmlns:x='X'><b>B</b></a>
xquery /a[b = 'B']
close
Related: If namespace detection is improved, . can be rewritten to text().
<commands>
<create-db name='db'>
<a xmlns='ns'>A</a>
</create-db>
<xquery>*:a[. = 'A']</xquery>
</commands>