Changes

Jump to navigation Jump to search
57 bytes added ,  16:32, 27 February 2020
no edit summary
You can access multiple databases in a single query:
<pre classsyntaxhighlight lang="brush:xquery">
for $i in 1 to 100
return db:open('books' || $i)//book/title
</presyntaxhighlight>
If the [[Options#DEFAULTDB{{Option|DEFAULTDB]] }} option is turned on, the path argument of the {{Code|fn:doc}} or {{Code|fn:collection}} function will first be resolved against the globally opened database.
Two more functions are available for retrieving information on database nodes:
The {{Code|fn:document-uri}} and {{Code|fn:base-uri}} functions return URIs that can also be reused as arguments for the {{Code|fn:doc}} and {{Code|fn:collection}} functions. As a result, the following example query always returns {{Code|true}}:
<pre classsyntaxhighlight lang="brush:xquery">
every $c in collection('anyDB')
satisfies doc-available(document-uri($c))
</presyntaxhighlight>
If the argument of {{Code|fn:doc}} or {{Code|fn:collection}} does not start with a valid database name, or if the addressed database does not exist, the string is interpreted as URI reference, and the documents found at this location will be returned. Examples:
* {{Code|doc("http://web.de")}}: retrieves the addressed URI and returns it as a main-memory document node.
* {{Code|doc("myfile.xml")}}: retrieves the given file from the file system and returns it as a main-memory document node. Note that updates to main-memory nodes are not automatically written back to disk unless the <code>[[Options#WRITEBACK{{Option|WRITEBACK]]</code> }} option is set.
* {{Code|collection("/path/to/docs")}}: returns a main-memory collection with all XML documents found at the addressed file path.
If the API you use does not support binary output (this is e.g. the case for various [[Clients|Client]] language bindings), you need to convert your binary data to its string representation before returning it to the client:
<pre classsyntaxhighlight lang="brush:xquery">
string(db:retrieve('multimedia', 'sample.avi'))
</presyntaxhighlight>
==HTTP Services==
The following commands create an empty database, add two resources, explicitly flush data structures to disk, and finally delete all inserted data:
<presyntaxhighlight>
CREATE DB example
SET AUTOFLUSH false
FLUSH
DELETE /
</presyntaxhighlight>
You may also use the BaseX-specific [[Database Module|XQuery Database Functions]] to create, add, replace, and delete XML documents:
<pre classsyntaxhighlight lang="brush:xquery">
let $root := "/path/to/xml/documents/"
for $file in file:list($root)
return db:add("database", $root || $file)
</presyntaxhighlight>
Last but not least, XML documents can also be added via the GUI and the ''Database'' menu.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu