Hi Florian,
Now I want to use them in a RESTXQ handler. Loading the document using "db:open" will return me a document-node() but I'd like a map instead for simpler use and automatic types (I'll be outputing JSON again).
As BaseX is an XML database in its very core, all documents will be stored as XML, so…
parse-json(xml-to-json(db:open(...)))
…is indeed what you need to do if you prefer the map representation. If you have more than a simple XQuery expression, the usual (and maybe obvious) solution for this is to wrap your code into utility modules:
module namespace util = 'util'; declare function util:to-json($doc as document-node()) as map(*) { parse-json(xml-to-json($doc) };
...
I guess it mostly depends on how you want to use BaseX: If you want to use it mainly as a document store, this might be the right choice. In that case, if you don’t need any advanced query features, you could even store your documents as binary data… Or eventually switch to a JSON database.
If you want to benefit from the full range of database and query optimizations, it is essential to use the XML representation. As an example, the following query…
//name[text() = 'John']
can be evaluated in nearly constant time, even if you have millions of documents (and if your indexes are up-to-date). With maps, you could do something like…
map:find($docs, 'abc')
…but this will always lead to a sequential scan.
Hope this helps, Christian