The problem is that for my application, I require pure xpath expressions (we have a GUI which is configurable by defining tuples of 1. a xpath expression to address data and 2. the desired gui / widget representation).
If your application supports functions, or if it's compliant with XPath 2.0, you could use doc() and collection() inside your query:
collection('db')//a1[@id = collection('db')//b/b1/@ref]
As an alternative, you could store your documents in a single database:
create db test1 <root/> insert node <a><a1 id='1'/><b1 ref='1'/></a> into / insert node <b><b1 ref='1'/></b> into / optimize (...optional) xquery //a1[@id=//b/b1/@ref]
This makes no real difference to BaseX, but it may not be an option in your application (?).
Christian