From time to time, I find myself wanting to run BaseX server commands from a shell script. For example, if I have two or three documents I'd like to add to a database, it's easy enough to do so from the GUI interface, or from the interactive server prompt. But if it's five or ten documents (and even more so if it's a few hundred or a few thousand documents), entering the commands interactively can become tedious and error prone. [...]
Here are two approaches that might do what you need:
a) you can separate multiple commands by semi-colons. an example:
basex -c "create db test <a/>; info db; drop db test"
b) commands can also be copied to an extra file and then redirected to basex:
basex < basex-commands.txt
# I'd use ADD to a.xml http://example.com/docs/a.xml, but # that doesn't seem to have the effect I expect.
This should work as well (at least with the latest version I'm having here):
basex -c "create db db; add to 123.xml http://files.basex.org/BaseXPADFile.xml; list db" Input Path Type Content-Type Size --------------------------------------- 123.xml xml application/xml 245
..what happens in your case?
Perhaps this is when I should buckle down and learn how to do this kind of thing with XQuery Update?
;) Unfortunately, that's not possible with XQuery Update anyway, as there's no way to add new documents to a collection (but for some reason it's actually legal to add several root elements to a document node). As a matter of fact, we are internally using exactly the same update operations for adding documents to a database, which can be called from the database commands and our XQuery database module [1]. This is a small example for adding 10,000 dummy documents to a database 'DB':
for $i in 1 to 10000 let $doc := document { <xml>{ $i }</xml> } return db:add('DB', $doc, concat($i, '.xml'))
Feel free to ask for more, Christian