Hey there,
I'm probably missing something but please bear with my as I'm new to XML databases and XQuery.
So what I've done is put JSON documents into BaseX using the REST interface and PUT requests.
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).
The only thing I could come up with and that works is using "Content-Type: application/json;format=basic" when uploading and then:
parse-json(xml-to-json(db:open(...)))
But that seems very complicated and wasteful. Is there no other way?
Regards Florian Wagner --- Universitaetsbibliothek Tuebingen Wilhelmstr. 32, 72074 Tuebingen IT-Abteilung, Raum B109 +49(0)7071/29-77860 http://www.ub.uni-tuebingen.de
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
basex-talk@mailman.uni-konstanz.de