https://docs.basex.org/api.php?action=feedcontributions&user=Marco+Lettere&feedformat=atomBaseX Documentation - User contributions [en]2024-03-29T11:09:01ZUser contributionsMediaWiki 1.34.0https://docs.basex.org/index.php?title=REST&diff=15527REST2021-10-11T16:30:27Z<p>Marco Lettere: Typo in external declaration of $x and $y instead of $a and $b which are used throughout the example.</p>
<hr />
<div>This page presents one of the [[Web Application]] services. It describes how to use the REST API of BaseX.<br />
<br />
BaseX offers a RESTful API for accessing database resources via URLs.<br />
REST ([https://en.wikipedia.org/wiki/Representational_State_Transfer REpresentational State Transfer])<br />
facilitates a simple and fast access to databases through HTTP. The HTTP methods<br />
GET, PUT, DELETE, and POST can be used to interact with the database.<br />
<br />
=Usage=<br />
<br />
By default, REST services are available at {{Code|http://localhost:8984/rest/}}. If no default credentials are specified in the URL or when starting the web application, they will be requested by the client ([[Web Application#User Management|see further]]).<br />
<br />
A web browser can be used to perform simple GET-based REST requests and display the response. Some alternatives for using REST are listed in the [[#Usage Examples|Usage Examples]].<br />
<br />
=URL Architecture=<br />
<br />
The root URL lists all available databases. The following examples assume that<br />
you have created a database instance from the [https://files.basex.org/xml/factbook.xml factbook.xml] document:<br />
<br />
:<code>http://localhost:8984/rest</code><br />
<br />
<syntaxhighlight lang="xml"><br />
<rest:databases resources="1" xmlns:rest="http://basex.org/rest"><br />
<rest:database resources="1" size="1813599">factbook</rest:database><br />
</rest:databases><br />
</syntaxhighlight><br />
<br />
The resources of a database can be listed by specifying the database, and potential sub directories, in the URL.<br />
In the given example, a single XML document is stored in the ''factbook'' database:<br />
<br />
:<code>http://localhost:8984/rest/factbook</code><br />
<br />
<syntaxhighlight lang="xml"><br />
<rest:database name="factbook" resources="1" xmlns:rest="http://basex.org/rest"><br />
<rest:resource type="xml" content-type="application/xml" size="77192">factbook.xml</rest:resource><br />
</rest:database><br />
</syntaxhighlight><br />
<br />
The contents of a database can be retrieved by directly addressing the resource:<br />
<br />
:<code>http://localhost:8984/rest/factbook/factbook.xml</code><br />
<br />
If a resource is not found, an HTTP response will be generated with <code>404</code> as status code.<br />
<br />
==Parameters==<br />
<br />
The following '''parameters''' can be applied to the operations:<br />
<br />
* '''Variables''':<br/>External variables can be ''bound'' before a query is evaluated ([[REST#Assigning Variables|see below]] for more).<br />
* '''Context''':<br/>The <code>context</code> parameter may be used to provide an initial XML context node.<br />
* '''Options''':<br/>Specified [[Options]] are applied before the actual operation will be performed.<br />
* '''Serialization''':<br/>All [[Serialization]] parameters known to BaseX can be specified as query parameters. Parameters that are specified within a query will be interpreted by the REST server before the output is generated.<br />
<br />
While '''Options''' can be specified for all operations, the remaining parameters will only make sense for '''Query''' and '''Run'''.<br />
<br />
=Request=<br />
<br />
==GET Method==<br />
<br />
If the GET method is used, all query parameters are directly specified within the URL.<br />
Additionally, the following '''operations''' can be specified:<br />
<br />
* {{Code|query}}: Evaluate an XQuery expression. If a database or database path is specified in the URL, it is set as query context.<br />
* {{Code|command}}: Execute a single [[Commands|database command]].<br />
* {{Code|run}}: Evaluate an XQuery file or command script located on the server. The file path is resolved against the directory specified by {{Option|RESTPATH}} (before, it was resolved against {{Option|WEBPATH}}). Similar to {{Code|query}}, a database or database path is set as context.<br />
<br />
; Examples<br />
<br />
* Lists all resources found in the '''tmp''' path of the ''factbook'' database:<br/><code>http://localhost:8984/rest/factbook/tmp</code><br />
<br />
* Returns the number of documents in a database:<br/><code>http://localhost:8984/rest/database?query=count(.)</code><br />
<br />
* Serializes a document as JSONML:<br/><code>http://localhost:8984/rest/factbook/factbook.xml?method=json&json=format=jsonml</code><br />
<br />
* <code>US-ASCII</code> is chosen as output encoding, and the query <code>eval.xq</code> is evaluated:<br/><code>http://localhost:8984/rest?run=eval.xq&encoding=US-ASCII</code><br />
<br />
* The next URL lists all database users that are known to BaseX:<br/><code>http://localhost:8984/rest?command=show+users</code><br />
<br />
==POST Method==<br />
<br />
The body of a POST request is interpreted as XML fragment, which specifies the operation to perform.<br />
The name of the root element determines how the body will be evaluated:<br />
<br />
* {{Code|commands}}: Run [[Commands#Command Scripts|Command Script]]<br />
* {{Code|query}}: Execute XQuery expression<br />
* {{Code|run}}: Run server-side file (query or command script)<br />
* {{Code|command}}: Execute single command <br />
<br />
The root element may be bound to the optional REST namespace. Existing command scripts can be sent to the server without any modifications:<br />
<br />
* Create an empty database and return database information: <br />
<syntaxhighlight lang="xml"><br />
<commands><br />
<create-db name='db'/><br />
<info-db/><br />
</commands><br />
</syntaxhighlight><br />
<br />
For the other commands, the following child elements are supported:<br />
<br />
{| class="wikitable"<br />
|-<br />
! Name<br />
! Description<br />
|-<br />
| {{Code|text}}<br />
| Required; contains the query string, command string, or file to be run<br />
|-<br />
| {{Code|parameter}}<br />
| Serialization parameter (with {{Code|@name}} and {{Code|@value}} attributes)<br />
|-<br />
| {{Code|option}}<br />
| Database option (with {{Code|@name}} and {{Code|@value}} attributes)<br />
|-<br />
| {{Code|variable}}<br />
| Variable bindings<br />
|-<br />
| {{Code|context}}<br />
| Initial context item<br />
|}<br />
<br />
; Examples<br />
<br />
* Return the first five city names of the <b>factbook</b> database:<br />
<syntaxhighlight lang="xml"><br />
<rest:query xmlns:rest="http://basex.org/rest"><br />
<rest:text><![CDATA[ (//city/name)[position() <= 5] ]]></rest:text><br />
</rest:query><br />
</syntaxhighlight><br />
<br />
* Return string lengths of all text nodes that are found in the node that has been specified as initial context node:<br />
<syntaxhighlight lang="xml"><br />
<query><br />
<text>for $i in .//text() return string-length($i)</text><br />
<context><br />
<xml><br />
<text>Hello</text><br />
<text>World</text><br />
</xml><br />
</context><br />
</query><br />
</syntaxhighlight><br />
<br />
* Return the registered database users encoded in <code>ISO-8859-1</code>:<br />
<syntaxhighlight lang="xml"><br />
<command><br />
<text>show users</text><br />
<parameter name='encoding' value='ISO-8859-1'/><br />
</command><br />
</syntaxhighlight><br />
<br />
* Create a new database from the specified input and preserve all whitespaces:<br />
<syntaxhighlight lang="xml"><br />
<command><br />
<text>create db test http://files.basex.org/xml/xmark.xml</text><br />
<option name='chop' value='false'/><br />
</command><br />
</syntaxhighlight><br />
<br />
* Bind value to the {{Code|$person}} variable and run query <code>find-person.xq</code>, which must be located in the directory specified by {{Option|WEBPATH}}:<br />
<syntaxhighlight lang="xml"><br />
<run><br />
<variable name='person' value='Johannes Müller'/><br />
<text>find-person.xq</text><br />
</run><br />
</syntaxhighlight><br />
<br />
==PUT Method==<br />
<br />
The PUT method is used to create new databases, or to add or update existing database resources:<br />
<br />
* '''Create Database''':<br/>A new database is created if the URL only specifies the name of a database. If the request body contains XML, a single document is created, adopting the name of the database.<br />
* '''Store Resource''':<br/>A resource is added to the database if the URL contains a database path. If the addressed resource already exists, it is replaced by the new input.<br />
<br />
There are two ways to store non-XML data in BaseX:<br />
<br />
* '''Store as Raw Data''':<br/> If <code>application/octet-stream</code> is chosen as content-type, the input is added as [[Binary Data]].<br />
* '''Convert to XML''':<br/> Incoming data is converted to XML if a parser is available for the specified content-type. The following content types are supported:<br />
** <code>application/json</code>: Stores JSON as XML.<br />
** <code>text/plain</code>: Stores plain text input as XML.<br />
** <code>text/comma-separated-values</code>: Stores CSV text input as XML. <br />
** <code>text/html</code>: Stores HTML input as XML.<br />
<br />
Conversion can be influenced by specifying additional content-type parameters (see [[RESTXQ#Content Types|RESTXQ]] for more information).<br />
<br />
If raw data is added and if no content type, or a wrong content, is specified, a <code>400</code> (BAD REQUEST) error will be raised.<br />
<br />
; Examples<br />
<br />
* A new database with the name <b>XMark</b> is created. If XML input is sent in the HTTP body, the resulting database resource will be called <b>XMark.xml</b>:<br/><code><nowiki>http://localhost:8984/rest/XMark</nowiki></code><br />
* A new database is created, and no whitespaces will be removed from the passed on XML input:<br/><code><nowiki>http://localhost:8984/rest/XMark?chop=false</nowiki></code><br />
* The contents of the HTTP body will be taken as input for the document <b>one.xml</b>, which will be stored in the <b>XMark</b> database:<br/><code><nowiki>http://localhost:8984/rest/XMark/one.xml</nowiki></code><br />
<br />
An HTTP response with status code <code>201</code> (CREATED)<br />
is sent back if the operation was successful. Otherwise,<br />
the server will reply with <code>404</code> (if a specified<br />
database was not found) or <code>400</code> (if the operation<br />
could not be completed).<br />
<br />
Have a look at the [[REST#Usage Examples|usage examples]] for more detailed examples using Java and shell tools like cURL.<br />
<br />
==DELETE Method==<br />
<br />
The DELETE method is used to delete databases or resources within a database.<br />
<br />
; Example<br />
* The <b>factbook</b> database is deleted:<br/><code><nowiki>http://localhost:8984/rest/factbook</nowiki></code><br />
* All resources of the <b>XMark</b> database are deleted that reside in the <b>tmp</b> path:<br/><code><nowiki>http://localhost:8984/rest/XMark/tmp/</nowiki></code><br />
<br />
The HTTP status code <code>404</code> is returned if no database is specified.<br />
<code>200</code> (OK) will be sent in all other cases.<br />
<br />
=Assigning Variables=<br />
<br />
==GET Method==<br />
<br />
All query parameters that have not been processed before will be treated as variable assignments:<br />
<br />
; Example<br />
<br />
* The following request assigns two variables to a server-side query file <code>mult.xq</code> placed in the HTTP directory:<br/><code>http://localhost:8984/rest?run=mult.xq&$a=21&$b=2</code><br />
<syntaxhighlight lang="xquery"><br />
(: XQuery file: mult.xq :)<br />
declare variable $a as xs:integer external;<br />
declare variable $b as xs:integer external;<br />
<mult>{ $a * $b }</mult><br />
</syntaxhighlight><br />
<br />
The dollar sign can be omitted as long as the variable name does not equal a parameter keyword (e.g.: <code>method</code>).<br />
<br />
==POST Method==<br />
<br />
If <code>query</code> or <code>run</code> is used as operation, external variables can be specified via the <code><variable/></code> element:<br />
<br />
<syntaxhighlight lang="xml"><br />
<query xmlns="http://basex.org/rest"><br />
<text><![CDATA[<br />
declare variable $a as xs:integer external;<br />
declare variable $b as xs:integer external;<br />
<mult>{ $a * $b }</mult><br />
]]></text><br />
<variable name="a" value="21"/><br />
<variable name="b" value="2"/><br />
</query><br />
</syntaxhighlight><br />
<br />
=Response=<br />
<br />
==Content Type==<br />
<br />
As the content type of a REST response cannot necessarily be dynamically determined, it can be enforced by the user. The final content type of a REST response is chosen as follows:<br />
<br />
# If the serialization parameter <code>[[Serialization|media-type]]</code> is supplied, it will be adopted as content-type.<br />
# Otherwise, if the serialization parameter <code>[[Serialization|method]]</code> is supplied, the content-type will be chosen according to the following mapping:<br />
#* <code>xml</code>, <code>adaptive</code>, <code>basex</code> → <code>application/xml</code><br />
#* <code>xhtml</code> → <code>text/html</code><br />
#* <code>html</code> → <code>text/html</code><br />
#* <code>text</code> → <code>text/plain</code><br />
#* <code>json</code> → <code>application/json</code><br />
# If no media-type or serialization method is supplied, the content type of a response depends on the chosen REST operation:<br />
#* '''Query'''/'''Run''' → <code>application/xml</code><br />
#* '''Command''' → <code>text/plain</code><br />
#* '''Get''' → <code>application/xml</code>, or content type of the addressed resource<br />
<br />
Serialization parameters can either be supplied as [[#Parameters|query parameters]] or within the query.<br />
<br />
The following three example requests all return <code>&lt;a/&gt;</code> with <code>application/xml</code> as content-type:<br />
<br />
:<code>http://localhost:8984/rest?query=%3Ca/%3E</code><br/><code>http://localhost:8984/rest?query=%3Ca/%3E&method=xml</code><br/><code>http://localhost:8984/rest?query=%3Ca/%3E&media-type=application/xml</code><br />
<br />
=Usage Examples=<br />
<br />
==Java==<br />
<br />
===Authentication===<br />
<br />
Most programming languages offer libraries to communicate with HTTP servers.<br />
The following example demonstrates how easy it is to perform a DELETE request with Java.<br />
<br />
Basic access authentication can be activated in Java by adding an authorization header<br />
to the <code>HttpURLConnection</code> instance. The header contains the word<br />
<code>Basic</code>, which specifies the authentication method, followed by the<br />
Base64-encoded <code>USER:PASSWORD</code> pair. As Java does not include a default<br />
conversion library for Base64 data, the internal BaseX class<br />
<code>org.basex.util.Base64</code> can be used for that purpose:<br />
<br />
<syntaxhighlight lang="java"><br />
import java.net.*;<br />
import org.basex.util.*;<br />
<br />
public final class RESTExample {<br />
public static void main(String[] args) throws Exception {<br />
// The java URL connection to the resource. <br />
URL url = new URL("http://localhost:8984/rest/factbook"); <br />
<br />
// Establish the connection to the URL. <br />
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); <br />
// Set as DELETE request. <br />
conn.setRequestMethod("DELETE"); <br />
<br />
// User and password.<br />
String user = "bob";<br />
String pw ="alice";<br />
// Encode user name and password pair with a base64 implementation.<br />
String encoded = Base64.encode(user + ":" + pw);<br />
// Basic access authentication header to connection request.<br />
conn.setRequestProperty("Authorization", "Basic " + encoded);<br />
<br />
// Print the HTTP response code. <br />
System.out.println("HTTP response: " + conn.getResponseCode()); <br />
<br />
// Close connection. <br />
conn.disconnect(); <br />
}<br />
}<br />
</syntaxhighlight><br />
<br />
===Content-Types===<br />
<br />
The content-type of the input can easily be included, just add the following property to<br />
the connection (in this example we explicitly store the input file as raw):<br />
<br />
<syntaxhighlight lang="java"><br />
// store input as raw<br />
conn.setRequestProperty("Content-Type", "application/octet-stream");<br />
</syntaxhighlight><br />
<br />
See the [[REST#PUT Requests|PUT Requests]] section for a description of the possible content-types.<br />
<br />
Find Java examples for all methods here:<br />
[https://github.com/BaseXdb/basex-examples/tree/master/src/main/java/org/basex/examples/rest/RESTGet.java GET], [https://github.com/BaseXdb/basex-examples/tree/master/src/main/java/org/basex/examples/rest/RESTPost.java POST], [https://github.com/BaseXdb/basex-examples/tree/master/src/main/java/org/basex/examples/rest/RESTPut.java PUT], [https://github.com/BaseXdb/basex-examples/tree/master/src/main/java/org/basex/examples/rest/RESTDelete.java DELETE].<br />
<br />
==Command Line== <br />
<br />
Tools such as the Linux commands [https://www.gnu.org/s/wget/ Wget] or [http://curl.haxx.se/ cURL] exist to<br />
perform HTTP requests (try copy &amp; paste):<br />
<br />
;GET <br />
* <code><nowiki>curl -i "http://localhost:8984/rest/factbook?query=//city/name"</nowiki></code><br />
;POST<br />
* <code><nowiki>curl -i -X POST -H "Content-Type: application/xml" -d "&lt;query xmlns='http://basex.org/rest'&gt;&lt;text&gt;//city/name&lt;/text&gt;&lt;/query&gt;" "http://localhost:8984/rest/factbook"</nowiki></code><br />
* <code><nowiki>curl -i -X POST -H "Content-Type: application/xml" -T query.xml "http://localhost:8984/rest/factbook"</nowiki></code> <br />
;PUT<br />
* <code><nowiki>curl -i -X PUT -T "etc/xml/factbook.xml" "http://localhost:8984/rest/factbook"</nowiki></code><br />
* <code><nowiki>curl -i -X PUT -H "Content-Type: application/json" -T "plain.json" "http://localhost:8984/rest/plain"</nowiki></code><br />
;DELETE<br />
* <code><nowiki>curl -i -X DELETE "http://admin:admin@localhost:8984/rest/factbook"</nowiki></code><br />
<br />
=Changelog=<br />
<br />
;Version 9.0<br />
* Added: Support for command scripts in the [[#POST Method|POST Method]].<br />
* Updated: The REST namespace in the [[#POST Method|POST Method]] has become optional.<br />
<br />
;Version 8.1<br />
* Added: Support for input-specific content-type parameters<br />
* Updated: The [[#GET Method|run operation]] now resolves file paths against the {{Option|RESTPATH}} option.<br />
<br />
;Version 8.0<br />
* Removed: {{Code|wrap}} parameter<br />
<br />
;Version 7.9<br />
* Updated: Also evaluate command scripts via the <code>[[#GET Method|run]]</code> operation.<br />
<br />
;Version 7.2<br />
* Removed: Direct evaluation of adresses resources with <code>application/xquery</code> as content type<br />
<br />
;Version 7.1.1<br />
* Added: {{Code|options}} parameter for specifying database options<br />
<br />
;Version 7.1<br />
* Added: PUT request: automatic conversion to XML if known content type is specified<br />
<br />
;Version 7.0<br />
* REST API introduced, replacing the old JAX-RX API</div>Marco Letterehttps://docs.basex.org/index.php?title=Options&diff=15422Options2021-05-27T08:09:29Z<p>Marco Lettere: Fixed typo if should be is</p>
<hr />
<div>This page is linked from the [[Getting Started]] Section.<br />
<br />
The options listed on this page influence the way how database [[Commands|commands]] are executed and XQuery expressions are evaluated. Two kinds of options exist:<br />
<br />
* '''[[#Global Options|Global Options]]''' are valid for all BaseX instances in the same JVM. This is particularly relevant if you are working with the client/server architecture.<br />
* '''Local options''' (all remaining ones) are specific to a client or session.<br />
<br />
Values of options are either ''strings'', ''numbers'' or ''booleans''. Options are ''static'' and not bound to a single operation (for example, the next command). Various ways exist to access and change options:<br />
<br />
* The current value of an option can be requested with the {{Command|GET}} command. Local options can be changed via {{Command|SET}} (all global options, except for {{Option|DEBUG}}, can only be changed at startup time). If an option is of type ''boolean'', and if no value is specified, its current value will be inverted.<br />
<br />
* The {{Code|.basex}} [[Configuration#Configuration Files|configuration file]] is parsed by every new local BaseX instance. It contains all global options. Local options can be specified at the end of the file after the {{Code|Local Options}} comment:<br />
<br />
<syntaxhighlight lang="perl"><br />
# General Options<br />
DEBUG = false<br />
...<br />
<br />
# Local Options<br />
CHOP = false<br />
</syntaxhighlight><br />
<br />
* Initial values for global options can also be specified via system properties, which can e.g. be passed on with the [https://docs.oracle.com/en/java/javase/11/tools/java.html -D flag] on command line, or using [https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#setProperty(java.lang.String,java.lang.String) System.setProperty()] before creating a BaseX instance. The specified keys need to be prefixed with {{Code|org.basex.}}. An example:<br />
<br />
<syntaxhighlight lang="perl"><br />
java -Dorg.basex.CHOP=false -cp basex.jar org.basex.BaseX -c"get chop"<br />
CHOP: false<br />
</syntaxhighlight><br />
<br />
* If using the Mac OS X packaged application then global options can be set within the Info.plist file within the Contents folder of the application package. For example:<br />
<br />
<syntaxhighlight lang="xml"><br />
<key>JVMOptions</key><br />
<array><br />
<string>-Dorg.basex.CHOP=false</string><br />
</array><br />
</syntaxhighlight><br />
<br />
* In a [[Web Application]], the default can be adjusted in the {{Code|web.xml}} file as follows:<br />
<br />
<syntaxhighlight lang="xml"><br />
<context-param><br />
<param-name>org.basex.chop</param-name><br />
<param-value>false</param-value><br />
</context-param><br />
</syntaxhighlight><br />
<br />
* In XQuery, local options can be set via option declarations and [[XQuery Extensions#Pragmas|pragmas]].<br />
<br />
If options are changed by operations in the [[GUI]], the underlying commands will be listed in the [[GUI#Visualizations|Info View]].<br/><br/><br />
<br />
=Global Options=<br />
<br />
Global options are constants. They can only be set in the configuration file or via system properties (see above). One exception is the [[#debug|DEBUG]] option, which can also be changed at runtime by users with [[User Management|admin permissions]].<br />
<br />
==General Options==<br />
<br />
===DEBUG===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|DEBUG [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Sends internal debug info to STDERR. This option can be turned on to get additional information for development and debugging purposes. It can also be triggered on [[Command-Line Options#BaseX Standalone|command line]] via <code>-d</code>.<br />
|}<br />
<br />
===DBPATH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|DBPATH [path]}}<br />
|-<br />
| '''Default'''<br />
|<code><code>[[Configuration#Database Directory|{home}/data]]</code><br />
|-<br />
| '''Summary'''<br />
|Points to the directory in which all databases are located.<br />
|}<br />
<br />
===LOGPATH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LOGPATH [path]}}<br />
|-<br />
| '''Default'''<br />
|<code>.logs</code><br />
|-<br />
| '''Summary'''<br />
|Points to the directory in which all [[Logging|log files]] are stored. Relative paths will be resolved against the {{Option|DBPATH}} directory.<br />
|}<br />
<br />
===REPOPATH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|REPOPATH [path]}}<br />
|-<br />
| '''Default'''<br />
|<code>[[Configuration#Database Directory|{home}/repo]]</code><br />
|-<br />
| '''Summary'''<br />
|Points to the [[Repository]], in which all XQuery modules are located.<br />
|}<br />
<br />
===LANG===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LANG [language]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|English}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the interface language. Currently, seven languages are available: 'English', 'German', 'French', 'Dutch', 'Italian', 'Japanese', and 'Vietnamese'.<br />
|}<br />
<br />
===LANGKEY===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LANGKEY [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Prefixes all texts with the internal language keys. This option is helpful if BaseX is translated into another language, and if you want to see where particular texts are displayed.<br />
|}<br />
<br />
===FAIRLOCK===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|FAIRLOCK [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Defines the locking strategy:<br />
* By default, non-fair is used. Read transactions will be favored, and transactions that access no databases can be evaluated even if the limit of parallel transactions (specified via {{Option|PARALLEL}}) has been reached. This prevents update operations from blocking all other requests. For example, the DBA can further be used to see which jobs are running, even if the queue is full.<br />
* If fair locking is enabled, read and write transactions will be treated equally (first in, first out). This avoids starvation of update operations, and it should be used if the prompt evaluation of update operations is critical.<br />
|}<br />
<br />
===CACHETIMEOUT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|CACHETIMEOUT [seconds]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|3600}}<br />
|-<br />
| '''Summary'''<br />
|Specifies how many seconds the results of queries, which have been queued by the [[Jobs Module|asynchronously executed]], will be cached in main memory.<br />
|}<br />
<br />
==Client/Server Architecture==<br />
<br />
===HOST===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|HOST [host]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|localhost}}<br />
|-<br />
| '''Summary'''<br />
|This host name is used by the client when connecting to a server. This option can also be changed when running the client on [[Command-Line Options#BaseX Client|command line]] via <code>-n</code>.<br />
|}<br />
<br />
===PORT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PORT [port]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|1984}}<br />
|-<br />
| '''Summary'''<br />
|This port is used by the client when connecting to a server. This option can also be changed when running the client on [[Command-Line Options#BaseX Client|command line]] via <code>-p</code>.<br />
|}<br />
<br />
===SERVERPORT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|SERVERPORT [port]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|1984}}<br />
|-<br />
| '''Summary'''<br />
|This is the port the database server will be listening to. This option can also be changed when running the server on [[Command-Line Options#BaseX Server|command line]] via <code>-p</code>.<br />
|}<br />
<br />
===USER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|USER [name]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Represents a user name, which is used for accessing the server or an HTTP service:<br />
* The default value will be overwritten if a client specifies its own credentials.<br />
* If the default value is empty, login will only be possible if the client specifies credentials.<br />
* The option can also be changed on [[Command-Line Options#BaseX Client|command line]] via <code>-U</code>.<br />
|}<br />
<br />
===PASSWORD===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PASSWORD [password]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Represents a password, which is used for accessing the server:<br />
* The default value will be overwritten if a client specifies its own credentials.<br />
* If the default value is empty, login will only be possible if the client specifies credentials.<br />
* The option can also be changed on [[Command-Line Options#BaseX Client|command line]] via <code>-P</code>.<br />
* Please note that it is a security risk to specify your password in plain text.<br />
|}<br />
<br />
===AUTHMETHOD===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|AUTHMETHOD [method]}}<br />
|-<br />
| '''Default'''<br />
|''Basic''<br />
|-<br />
| '''Summary'''<br />
|Specifies the default authentication method, which will be used by the [[Web Application|HTTP server]] for negotiating credentials. Allowed values are {{Code|Basic}}, {{Code|Digest}}, and {{Code|Custom}}:<br/><br />
* If basic access is chosen, the client can still request digest authentication.<br />
* This is different for digest access, which cannot be overwritten.<br />
* With custom authentication, the server will not do any authentication.<br />
|}<br />
<br />
===SERVERHOST===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|SERVERHOST [host&#x7c;ip]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|This is the host name or ip address the server is bound to. If the option is set to an empty string (which is the default), the server will be open to all clients.<br />
|}<br />
<br />
===PROXYHOST===<br />
<br />
{| width='100%' width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PROXYHOST [host]}}<br />
|-<br />
| '''Default'''<br />
|''empty'' <br />
|-<br />
| '''Summary'''<br />
|This is the host name of a proxy server. If the value is an empty string, it will be ignored.<br />
|}<br />
<br />
===PROXYPORT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PROXYPORT [port]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|0}}<br />
|-<br />
| '''Summary'''<br />
|This is the port number of a proxy server. If the value is set to {{Code|0}}, it will be ignored.<br />
|}<br />
<br />
===NONPROXYHOSTS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|NONPROXYHOSTS [hosts]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|This is a list of hosts that should be directly accessed. If the value is an empty string, it will be ignored.<br />
|}<br />
<br />
===IGNOREHOSTNAME===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|IGNOREHOSTNAME [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If this option is enabled, hostnames of certificates will not be verified. Use {{Option|IGNORECERT}} to completely disable certificate verification.<br />
|}<br />
<br />
===IGNORECERT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|IGNORECERT [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|This option can be turned on to ignore untrusted certificates when connecting to servers. Use {{Option|IGNOREHOSTNAME}} to suppress only the hostname verification.<br />
|}<br />
<br />
===TIMEOUT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TIMEOUT [seconds]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|30}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the maximum time a transaction triggered by a client may take. If an operation takes longer than the specified number of seconds, it will be aborted. Active update operations will not be affected by this timeout, as this would corrupt the integrity of the database. The timeout is deactivated if the timeout is set to {{Code|0}}. It is ignored for operations with [[User Management|admin permissions]].<br />
|}<br />
<br />
===KEEPALIVE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|KEEPALIVE [seconds]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|600}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the maximum time a client will be remembered by the server. If there has been no interaction with a client for a longer time than specified by this timeout, it will be disconnected. Running operations will not be affected by this option. The keepalive check is deactivated if the value is set to {{Code|0}}.<br />
|}<br />
<br />
===PARALLEL===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PARALLEL [number]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|8}}<br />
|-<br />
| '''Summary'''<br />
|Denotes the maximum allowed number of parallel [[Transaction Management|transactions]]:<br />
* If {{Option|FAIRLOCK}} is enabled, the number of parallel transactions will never exceed the specified value.<br />
* If the option is disabled (which is the default), the limit only applies to transactions that access databases.<br />
* The main reason for allowing parallel operations is to prevent slow transactions from blocking all other operations. A higher number of parallel operations may increase disk activity and thus slow down queries. In some cases, a single transaction may even give you better results than any parallel activity.<br />
|}<br />
<br />
===LOG===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LOG [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Turns [[Logging]] of server operations and HTTP requests on/off. This option can also be changed when running the server on [[Command-Line Options#BaseX Server|command line]] via <code>-z</code>.<br />
|}<br />
<br />
===LOGMSGMAXLEN===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LOGMSGMAXLEN [length]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|1000}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the maximum length of a single [[Logging|log message]].<br />
|}<br />
<br />
===LOGTRACE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LOGTRACE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|If BaseX is running as [[Web Application]], trace output (generated via {{Code|fn:trace}}, {{Function|Profiling|prof:dump}} and similar functions) is written to the [[Logging|database logs]]. If this option is disabled, trace output will be redirected to standard error, as it is known from the standalone version of BaseX.<br />
|}<br />
<br />
==HTTP Services==<br />
<br />
Most HTTP options are defined in the {{Code|jetty.xml}} and {{Code|web.xml}} configuration files in the <code>[https://github.com/BaseXdb/basex/tree/master/basex-api/src/main/webapp/WEB-INF webapp/WEB-INF]</code> directory. Some additional BaseX-specific options exist that will be set before the web server is started:<br />
<br />
===WEBPATH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|WEBPATH [path]}}<br />
|-<br />
| '''Default'''<br />
|<code>[[Configuration#Database Directory|{home}/webapp]]</code><br />
|-<br />
| '''Summary'''<br />
|Points to the directory in which all the [[Web Application]] contents are stored, including XQuery, Script, [[RESTXQ]] and configuration files:<br />
* The option is ignored if BaseX is deployed as [[Web Application#Servlet_Container|web servlet]].<br />
* It cannot be assigned via the {{Code|web.xml}} file, as it will be evaluated before the configuration files are parsed.<br />
|}<br />
<br />
===GZIP===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|GZIP [boolean]}}<br />
|-<br />
| '''Default'''<br />
|<code>false</code><br />
|-<br />
| '''Summary'''<br />
|Jetty provides a [https://www.eclipse.org/jetty/documentation/current/gzip-filter.html Gzip handler] for dynamically uncompressing requests and compressing responses. This feature can be enabled if Jetty is started via the [[Web Application|BaseX HTTP Server]]:<br />
* The option can also be enabled on [[Command-Line Options#HTTP Server|command line]] via <code>-g</code>.<br />
* It cannot be assigned via the {{Code|web.xml}} file, as it will be evaluated before the configuration files are parsed.<br />
* The [https://github.com/eclipse/jetty.project/blob/7cc552013eb4d05cb603ba0bc85d176c97957cd4/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java#L187-L211 same defaults] of the web server will be applied (support for GET requests, exclusion of binaries, MSIE 6.0, etc.).<br />
|}<br />
<br />
===RESTXQPATH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|RESTXQPATH [path]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Points to the directory which contains the [[RESTXQ]] modules of a web application. Relative paths will be resolved against the {{Option|WEBPATH}} directory.<br />
|}<br />
<br />
===PARSERESTXQ===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PARSERESTXQ}}<br />
|-<br />
| '''Default'''<br />
|{{Code|3}}<br />
|-<br />
| '''Summary'''<br />
|Timeout after which the RESTXQ directory will be parsed for changes:<br />
* If {{Code|0}} is specified, the directory will be parsed every time a RESTXQ function is called.<br />
* A positive value defines the idle time in seconds after which parsing will be enforced. The default value is {{Code|3}}: Changes in the RESTXQ directory will be detected after 3 seconds without RESTXQ function calls.<br />
* Monitoring is completely disabled if a negative value is specified.<br />
<br />
See [[RESTXQ#Preliminaries|RESTXQ Preliminaries]] for more details.<br />
|}<br />
<br />
===RESTXQERRORS===<br />
<br />
{{Mark|Updated with BaseX 9.5:}} Additionally suppress stack trace in HTTP response.<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|RESTXQERRORS}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Reports parsing errors in XQuery modules in the RESTXQ directory and returns the full error message and stack trace to the client. By default, this option is enabled. In a production environment, it can be disabled to suppress errors that should not be seen by the user of an API (the full error information can still be looked up in the database logs). See [[RESTXQ#Error Handling|RESTXQ Error Handling]] for more details.<br />
|}<br />
<br />
===RESTPATH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|RESTPATH [path]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Points to the directory which contains XQuery files and command scripts, which can be evaluated via the [[REST#GET Requests|REST run operation]]. Relative paths will be resolved against the {{Option|WEBPATH}} directory.<br />
|}<br />
<br />
===HTTPLOCAL===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|HTTPLOCAL [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|By default, if BaseX is run as [[Web Application]], the database server instance will be started in addition, which can then be addressed by [[Clients]] via the database port (see {{Option|PORT}}).<br/>If the option is set to {{Code|true}}, no database server will be launched.<br />
|}<br />
<br />
===STOPPORT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|STOPPORT [port]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|8985}}<br />
|-<br />
| '''Summary'''<br />
|This is the port on which the [[Startup#BaseX HTTP Server|HTTP Server]] can be locally closed:<br />
* The listener for stopping the web server will only be started if the specified value is greater than {{Code|0}}.<br />
* The option is ignored if BaseX is used as a [[Web Application]] or started via [[Web Application#Maven|Maven]].<br />
* This option can also be changed when running the HTTP server on [[Command-Line Options#BaseX Server|command line]] via <code>-s</code>.<br />
|}<br />
<br />
=Create Options=<br />
<br />
==General==<br />
<br />
===MAINMEM===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|MAINMEM [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If this option is turned on, new databases will be created in main memory:<br />
* Most queries will be evaluated faster in main-memory mode, but all data is lost if the BaseX instance in which the database was created is shut down.<br />
* It is not possible to store binary resources in a main-memory database.<br />
* A main-memory database will have no disk representation. However, it is possible to export the database via the {{Command|EXPORT}} command, and create a new database from the exported file in a second step.<br />
* This option will not be available for [[Database Module#db:create|db:create]], because the database would not be accessible anymore after database creation, i. e., outside the query scope.<br />
|}<br />
<br />
===ADDCACHE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ADDCACHE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If this option is activated, data structures of documents will first be cached to disk before being added to the final database. This option is helpful when larger documents need to be added, and if the existing heuristics cannot estimate the input size (e.g. when adding directories or sending input streams).<br />
|}<br />
<br />
==Parsing==<br />
<br />
===CREATEFILTER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|CREATEFILTER [filter]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|*.xml}}<br />
|-<br />
| '''Summary'''<br />
|File filter in the [[Commands#Glob Syntax|Glob Syntax]], which is applied whenever new databases are created, or resources are added to a database.<br />
|}<br />
<br />
===ADDARCHIVES===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ADDARCHIVES [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|If this option is set to {{Code|true}}, files within archives (ZIP, GZIP, TAR, TGZ, DOCX, etc.) are parsed whenever new databases are created or resources are added to a database.<br />
|}<br />
<br />
===ARCHIVENAME===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ARCHIVENAME [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If this option is set to {{Code|true}}, the file name of parsed archives will be included in the document paths.<br />
|}<br />
<br />
===SKIPCORRUPT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|SKIPCORRUPT [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Skips corrupt (i.e., not well-formed) files while creating a database or adding new documents. If this option is activated, document updates are slowed down, as all files will be parsed twice. Next, main memory consumption will be higher as parsed files will be cached in main memory.<br />
|}<br />
<br />
===ADDRAW===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ADDRAW [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If this option is enabled, all resources that are filtered out by the {{Option|CREATEFILTER}} option while being added to a database will be stored as [[Binary Data|raw files]] instead (i.e., in their binary representation).<br />
|}<br />
<br />
===PARSER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|PARSER [type]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|XML}}<br />
|-<br />
| '''Summary'''<br />
|Defines a [[Parsers|parser]] for importing new files to the database. Available parsers are {{Code|XML}}, {{Code|JSON}}, {{Code|CSV}}, {{Code|TEXT}}, {{Code|HTML}}, and {{Code|RAW}}. HTML input will be parsed as XML documents if [[Parsers#HTML_Parser|Tagsoup]] is not found in the classpath.<br />
|}<br />
<br />
===CSVPARSER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|CSVPARSER [options]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Specifies the way how CSV data will be parsed. Keys and values are delimited with <code>=</code>, and multiple options are delimited with <code>,</code>. The available options (except for the additional <code>encoding</code> option) are described in the [[CSV Module#Options|CSV Module]].<br />
|-<br />
| '''Examples'''<br />
|<code>encoding=CP1252,header=true</code> parses the input as CP1252 and the first line as header.<br />
|}<br />
<br />
===JSONPARSER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|JSONPARSER [options]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Specifies the way how JSON data will be parsed. Keys and values are delimited with <code>=</code>, and multiple options are delimited with <code>,</code>. The available options (except for the additional <code>encoding</code> option) are described in the [[JSON Module#Options|JSON Module]].<br />
|-<br />
| '''Examples'''<br />
|<code>format=jsonml,lax=yes</code> interprets the input as JSONML and uses lax parsing.<br />
|}<br />
<br />
===HTMLPARSER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|HTMLPARSER [options]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Specifies the way how HTML data will be parsed. Keys and values are delimited with <code>=</code>, and multiple options are delimited with <code>,</code>. The available options are described in the [[Parsers#Options|Parsers]] article.<br />
|-<br />
| '''Examples'''<br />
|<br />
* <code>encoding=Shift-JIS,nons=true</code> parses the input as Sihft-JIS and suppresses namespaces.<br />
* <code>lexical=true</code> preserves comments.<br />
|}<br />
<br />
===TEXTPARSER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TEXTPARSER [options]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Specifies the way how TEXT data will be parsed. Keys and values are delimited with <code>=</code>, and multiple options are delimited with <code>,</code>. The available options are listed in the [[Parsers]] article.<br />
|-<br />
| '''Examples'''<br />
|<code>lines=true</code> creates a single element for each line of text.<br />
|}<br />
<br />
==XML Parsing==<br />
<br />
===CHOP===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|CHOP [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Many XML documents include whitespaces that have been added to improve readability. This option controls the [https://www.w3.org/TR/REC-xml/#sec-white-space white-space processing mode] of the XML parser:<br />
* With the default value {{Code|true}}, leading and trailing whitespaces from text nodes will be chopped and all empty text nodes will be discarded.<br />
* The flag should be turned off if a document contains [[Full-Text#Mixed Content|mixed content]].<br />
* The flag can also be turned off on [[Command-Line Options#BaseX Standalone|command line]] via <code>-w</code>.<br />
* If the <code>xml:space="preserve"</code> attribute is attached to an element, chopping will be turned off for all descendant text nodes. <br />
<br />
In the following example document, the whitespaces in the text nodes of the {{Code|text}} element will not be chopped:<br />
<syntaxhighlight lang="xml"><br />
<xml><br />
<title><br />
Demonstrating the CHOP flag<br />
</title><br />
<text xml:space="preserve">To <b>be</b>, or not to <b>be</b>, that is the question.</text><br />
</xml><br />
</syntaxhighlight><br />
It is recommendable to additionally assign <code>indent=no</code> to the {{Option|SERIALIZER}} option; otherwise the serialized documents will automatically be indented.<br />
|}<br />
<br />
===STRIPNS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|STRIPNS [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Strips all namespaces from an XML document and all elements while parsing.<br />
|}<br />
<br />
===INTPARSE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|INTPARSE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Uses the internal XML parser instead of the standard Java XML parser. Here are some reasons for using the internal parser:<br />
* Performance: Documents (in particular small ones) will be parsed faster<br />
* Fault tolerance: invalid characters will automatically be replaced with the Unicode replacement character <code>FFFD</code> (&#xFFFD;)<br />
* Entities: around 250 HTML entities will be detected and decoded<br />
You will be able to correctly parse most XML documents with the internal parser. Java’s Xerces parser is still used as default, however, because it supports all features of the XML standard and advanced DTD features, such as recursive entity expansion.<br />
|}<br />
<br />
===DTD===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|DTD [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Parses referenced DTDs and resolves XML entities. By default, this option is switched to {{Code|false}}, as many DTDs are located externally, which may completely block the process of creating new databases. The {{Option|CATFILE}} option can be changed to locally resolve DTDs.<br />
|}<br />
<br />
===XINCLUDE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|XINCLUDE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Resolves XInclude inclusion tags and merges referenced XML documents. By default, this option is switched to {{Code|true}}. This option is only available if the standard Java XML Parser is used (see {{Option|INTPARSE}}).<br />
|}<br />
<br />
===CATFILE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|CATFILE [path]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Semicolon-separated list of XML catalog files to resolve URIs. See [[Catalog Resolver]]s for more details.<br />
|}<br />
<br />
==Indexing==<br />
<br />
The following options control the creation of index structures. The current values will be considered if a new database is created. See [[Indexes]] for more details.<br />
<br />
===TEXTINDEX===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TEXTINDEX [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Creates a text index whenever a new database is created. A text index speeds up queries with equality comparisons on text nodes. See [[Index#Text Index|Text Index]] for more details.<br />
|}<br />
<br />
===ATTRINDEX===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ATTRINDEX [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Creates an attribute index whenever a new database is created. An attribute index speeds up queries with equality comparisons on attribute values. See [[Index#Attribute Index|Attribute Index]] for more details.<br />
|}<br />
<br />
===TOKENINDEX===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TOKENINDEX [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Creates a token index whenever a new database is created. A token index speeds up searches for single tokens in attribute values. See [[Index#Token Index|Token Index]] for more details.<br />
|}<br />
<br />
===FTINDEX===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|FTINDEX [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Creates a full-text index whenever a new database is created. A full-text index speeds up queries with full-text expressions. See [[Index#Full-Text Index|Full-Text Index]] for more details.<br />
|}<br />
<br />
===TEXTINCLUDE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TEXTINCLUDE [names]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Defines name patterns for the parent elements of texts that are indexed. By default, all text nodes will be indexed.<br/>Name patterns are separated by commas. See [[Indexes#Selective Indexing|Selective Indexing]] for more details.<br />
|}<br />
<br />
===ATTRINCLUDE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ATTRINCLUDE [names]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Defines name patterns for the attributes to be indexed. By default, all attribute nodes will be indexed.<br/>Name patterns are separated by commas. See [[Indexes#Selective Indexing|Selective Indexing]] for more details.<br />
|}<br />
<br />
===TOKENINCLUDE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TOKENINCLUDE [names]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Defines name patterns for the attributes to be indexed. By default, tokens in all attribute nodes will be indexed.<br/>Name patterns are separated by commas. See [[Indexes#Selective Indexing|Selective Indexing]] for more details.<br />
|}<br />
<br />
===FTINCLUDE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|FTINCLUDE [names]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Defines name patterns for the parent elements of texts that are indexed. By default, all text nodes will be indexed.<br/>Name patterns are separated by commas. See [[Indexes#Selective Indexing|Selective Indexing]] for more details.<br />
|}<br />
<br />
===MAXLEN===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|MAXLEN [int]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|96}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the maximum length for strings to be stored in [[Indexes|index structures]]. The value of this option will be assigned once to a new database, and can only be changed by creating a new database or doing a [[Commands#OPTIMIZE|full optimization]].<br />
|}<br />
<br />
===MAXCATS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|MAXCATS [int]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|100}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the maximum number of distinct values (categories) that will be stored together with the element/attribute names or unique paths in the [[Index#Name Index|Name Index]] or [[Index#Path Index|Path Index]]. The value of this option will be assigned once to a new database, and cannot be changed after that.<br />
|}<br />
<br />
===UPDINDEX===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|UPDINDEX [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If turned on, incremental indexing will be enabled:<br />
* The current value of this option will be assigned to new databases. It can be changed for existing databases by running {{Command|OPTIMIZE}} with the {{Code|ALL}} keyword or [[Database_Module#db:optimize|db:optimize($db, true())]].<br />
* After each update, the value indexes will be refreshed as well. Incremental updates are currently not available for the full-text index and database statistics.<br />
* Find more details in the article on [[Index#Updates|Index Structures]].<br />
|}<br />
<br />
===AUTOOPTIMIZE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|AUTOOPTIMIZE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If turned on, auto optimization will be applied to new databases:<br />
* With each update, outdated indexes and database statistics will be recreated.<br />
* As a result, the index structures will always be up-to-date.<br />
* However, updates can take much longer, so this option should only be activated for medium-sized databases.<br />
* The value of this option will be assigned once to a new database. It can be reassigned by running {{Command|OPTIMIZE}} or [[Database_Module#db:optimize|db:optimize]].<br />
|}<br />
<br />
===SPLITSIZE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|SPLITSIZE [num]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|0}}<br />
|-<br />
| '''Summary'''<br />
|This option affects the [[Indexes#Performance|construction]] of new value indexes. It controls the number of index build operations that are performed before writing partial index data to disk:<br />
* By default, if the value is set to {{Code|0}}, some heuristics are applied, based on the current memory consumption. Usually, this works fine.<br />
* If explicit garbage collection is disabled when running Java (e.g. via the JVM option {{Code|-XX:+DisableExplicitGC}}), you may need to choose a custom split size.<br />
* You can e. g. start with {{Code|1000000}} (one million) index operations and adjust this value in the next steps.<br />
* The larger the assigned value is, the less splits will take place, and the more main memory will be required.<br />
|}<br />
<br />
==Full-Text Indexing==<br />
<br />
===STEMMING===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|STEMMING [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If {{Code|true}}, all tokens will be stemmed during full-text indexing, using a language-specific stemmer implementation. By default, tokens will not be stemmed. See [[Indexes#Full-Text Index|Full-Text Index]] for more details.<br />
|}<br />
<br />
===CASESENS===<br />
<br />
{| width='100%'<br />
<br />
| width='120' | '''Signature'''<br />
|{{Code|CASESENS [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If {{Code|true}}, the case of tokens will be preserved during full-text indexing. By default, case will be ignored (all tokens will be indexed in lower case). See [[Indexes#Full-Text Index|Full-Text Index]] for more details.<br />
|}<br />
<br />
===DIACRITICS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|DIACRITICS [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If set to {{Code|true}}, diacritics will be preserved during full-text indexing. By default, diacritics will be removed. See [[Indexes#Full-Text Index|Full-Text Index]] for more details.<br />
|}<br />
<br />
===LANGUAGE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LANGUAGE [lang]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|en}}<br />
|-<br />
| '''Summary'''<br />
|The specified language will influence the way how texts will be tokenized and stemmed. It can be the name of a language or a language code. See [[Indexes#Full-Text Index|Full-Text Index]] for more details.<br />
|}<br />
<br />
===STOPWORDS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|STOPWORDS [path]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|If a text file with stop words is specified, frequently used terms contained in that file will be ignored when a full-text index is created. A stopword list may decrease the size of the full text index and speed up your queries. See [[Indexes#Full-Text Index|Full-Text Index]] for more details.<br />
|}<br />
<br />
=Query Options=<br />
<br />
===QUERYINFO===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|QUERYINFO [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Prints more information on internal query rewritings, optimizations, and performance. By default, this info is shown in the [[GUI#Visualizations|Info View]] in the GUI. It can also be activated on [[Command-Line Options#BaseX Standalone|command line]] via <code>-V</code>. <br />
|}<br />
<br />
===MIXUPDATES===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|MIXUPDATES}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Allows queries to both contain updating and non-updating expressions. All updating constraints will be turned off, and nodes to be returned will be copied before they are modified by an updating expression. By default, in compliance with the XQuery Update Facility, this option is set to {{Code|false}}. See [[XQuery Update#Returning Results|Returning Results]] for more details.<br />
|}<br />
<br />
===BINDINGS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|BINDINGS [vars]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Contains external variables to be bound to a query. The string must comply with the following rules:<br />
* Variable names and values must be separated by equality signs.<br />
* Multiple variables must be delimited by commas.<br />
* Commas in values must be duplicated.<br />
* Variables may optionally be introduced with a leading dollar sign.<br />
* If a variable uses a namespace different to the default namespace, it can be specified with the [http://www.jclark.com/xml/xmlns.htm Clark Notation] or [https://www.w3.org/TR/xquery-30/#id-basics Expanded QName Notation].<br />
This option can also be used on [[Command-Line Options#BaseX Standalone|command line]] with the flag <code>-b</code>.<br />
|-<br />
| '''Examples'''<br />
|<br />
* <code>$a=1,$b=2</code> &nbsp; binds the values {{Code|1}} and {{Code|2}} to the variables $a and $b<br />
* <code>a=1,,2</code> &nbsp; binds the value {{Code|1,2}} to the variable $a<br />
* <code>{URI}a=x</code> &nbsp; binds the value {{Code|x}} to the variable $a with the namespace {{Code|URI}}.<br />
* In the following [[Commands#Command_Scripts| Command Script]], the value {{Code|hello world!}} is bound to the variable {{Code|$GREETING}}:<br />
<syntaxhighlight lang="xquery"><br />
SET BINDINGS GREETING="hello world!"<br />
XQUERY declare variable $GREETING external; $GREETING<br />
</syntaxhighlight><br />
|}<br />
<br />
===INLINELIMIT===<br />
<br />
{{Mark|Updated with Version 9.5:}} default reduced to 50.<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|INLINELIMIT}}<br />
|-<br />
| '''Default'''<br />
|{{Code|50}}<br />
|-<br />
| '''Summary'''<br />
|This option controls inlining of XQuery functions:<br />
* The XQuery compiler inlines functions to speed up query evaluation.<br />
* Inlining will only take place if a function body is not too large (i.e., if it does not contain too many expressions).<br />
* With this option, this maximum number of expressions can be specified.<br />
* Function inlining can be turned off by setting the value to {{Code|0}}.<br />
* The limit can be locally overwritten via the [[XQuery Extensions#Function Inlining|%basex:inline]] annotation (follow the link to get more information on function inlining).<br />
|}<br />
<br />
===UNROLLLIMIT===<br />
<br />
{{Mark|Introduced with Version 9.6:}}<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|UNROLLLIMIT}}<br />
|-<br />
| '''Default'''<br />
|{{Code|5}}<br />
|-<br />
| '''Summary'''<br />
|This option controls the unroll limit:<br />
* Loops with few iterations are ''unrolled'' by the XQuery compiler to enable further optimizations.<br />
* If the limit is increased, more optimizations will take place, but the memory consumption and compile time will increase.<br />
* See [[XQuery Optimizations#Loop Unrolling|Loop Unrolling]] for more details.<br />
|}<br />
<br />
===ENFORCEINDEX===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|ENFORCEINDEX [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Enforces index rewritings in path expressions. See [[Indexes#Enforce Rewritings|Enforce Rewritings]] for details.<br />
|}<br />
<br />
===COPYNODE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|COPYNODE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|When creating new nodes in XQuery via [https://www.w3.org/TR/xquery-31/#id-constructors Node Constructors], all enclosed nodes will be copied, and all resulting nodes will get new node identities. This step can be very expensive, and it can be disabled with this option. The option should be used carefully, as it changes the standard behavior of XQuery. It should preferrably be used in [[XQuery Extensions#Database Pragmas|Pragmas]].<br />
|}<br />
<br />
===TAILCALLS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|TAILCALLS}}<br />
|-<br />
| '''Default'''<br />
|{{Code|256}}<br />
|-<br />
| '''Summary'''<br />
|Specifies how many stack frames of [https://en.wikipedia.org/wiki/Tail_call tail-calls] are allowed on the stack at any time. When this limit is reached, tail-call optimization takes place and some call frames are eliminated. The feature can be turned off by setting the value to {{Code|-1}}.<br />
|}<br />
<br />
===WITHDB===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|WITHDB}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|By default, resources specified via [[Databases#XML Documents|fn:doc]] and [[Databases#XML Documents|fn:collection]] are looked up both in the database and in the file system. If you always use {{Function|Database|db:open}} to access databases, it is recommendable to disable this option:<br />
* No locks will be created for the two functions (see [[Transaction Management#Limitations|limitations of database locking]] for more details).<br />
* Access to local and external resources will be faster, as the database lookup will be skipped.<br />
|}<br />
<br />
===DEFAULTDB===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|DEFAULTDB}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|If this option is turned on, paths specified in the [[Databases#XML Documents|fn:doc]] and [[Databases#XML Documents|fn:collection]] functions will first be resolved against a database that has been opened in the global context outside the query (e.g. by the {{Command|OPEN}} command). If the path does not match any existing resources, it will be resolved as described in the article on [[Databases#Access Resources|accessing database resources]].<br />
|}<br />
<br />
===FORCECREATE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|FORCECREATE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|By activating this option, database instances will be created with the XQuery functions [[Databases#XML Documents|fn:doc]] and [[Databases#XML Documents|fn:collection]].<br />
|}<br />
<br />
===CHECKSTRINGS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|CHECKSTRINGS [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|By default, characters from external sources that are invalid in XML will trigger an error. If the option is set to <code>false</code>, these characters will be replaced with the Unicode replacement character <code>FFFD</code> (&#xFFFD;). The option affects [[Java Bindings]] and string conversion and input functions such as [[Archive Module#archive:create|archive:create]], [[Archive Module#archive:extract-text|archive:extract-text]], [[Archive Module#archive:update|archive:update]], and [[ZIP Module#zip:text-entry|zip:text-entry]].<br />
|}<br />
<br />
===LSERROR===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|LSERROR [error]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|0}}<br />
|-<br />
| '''Summary'''<br />
|This option specifies the maximum Levenshtein error for fuzzy full-text matching. By default, if {{Code|0}} is assigned, the error value is calculated dynamically. See [[Full-Text#Fuzzy_Querying|Fuzzy Querying]] for more details.<br />
|}<br />
<br />
===RUNQUERY===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|RUNQUERY [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Specifies if a query will be executed or parsed only. This option can also be changed on [[Command-Line Options#BaseX Standalone|command line]] via <code>-R</code>.<br />
|}<br />
<br />
===RUNS===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|RUNS [num]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|1}}<br />
|-<br />
| '''Summary'''<br />
|Specifies how often a query will be evaluated. The result is serialized only once, and the measured times are averages of all runs. This option can also be changed on [[Command-Line Options#BaseX Standalone|command line]] via <code>-r</code>.<br />
|}<br />
<br />
=Serialization Options=<br />
<br />
===SERIALIZE===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|SERIALIZE [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Results of XQuery expressions will be serialized if this option is turned on. For debugging purposes and performance measurements, this option can be set to {{Code|false}}. It can also be turned off on [[Command-Line Options#BaseX Standalone|command line]] via <code>-z</code>. <br />
|}<br />
<br />
===SERIALIZER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|SERIALIZER [params]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Parameters for [[Serialization|serializing]] query results. The string must comply with the following rules:<br />
* Variable names and values must be separated by equality signs.<br />
* Multiple variables must be delimited by commas.<br />
* Commas in values must be duplicated.<br />
The option can also be used on [[Command-Line Options#BaseX Standalone|command line]] with the flag <code>-s</code>.<br />
|-<br />
| '''Examples'''<br />
|<br />
* <code>indent=no</code> : disables automatic indentation of XML nodes. This is usually a good choice when working with [[Full-Text#Mixed Content|Mixed-Content Data]].<br />
* <code>encoding=US-ASCII,omit-xml-declaration=no</code> : sets the encoding to {{Code|US-ASCII}} and prints the XML declaration.<br />
* <code>item-separator=,,</code> : separates serialized items by a single comma.<br />
|}<br />
<br />
===EXPORTER===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|EXPORTER [params]}}<br />
|-<br />
| '''Default'''<br />
|''empty''<br />
|-<br />
| '''Summary'''<br />
|Contains parameters for exporting resources of a database and writing files after updates via the {{Option|WRITEBACK}} option. Keys and values are separated by equality signs, multiple parameters are delimited by commas. See [[Serialization]] for more details.<br />
|-<br />
| '''Examples'''<br />
|<br />
* <code>indent=no,omit-xml-declaration=no</code> : disables automatic indentation of XML nodes, outputs the XML declaration.<br />
|}<br />
<br />
===XMLPLAN===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|XMLPLAN [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Prints the execution plan of an XQuery expression in its XML representation. This option can also be activated on [[Command-Line Options#BaseX Standalone|command line]] via <code>-x</code>. <br />
|}<br />
<br />
===COMPPLAN===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|COMPPLAN [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Generates the query plan, which can be activated via {{Option|XMLPLAN}}, before or after query compilation. This option can also be activated on [[Command-Line Options#BaseX Standalone|command line]] via <code>-X</code>. <br />
|}<br />
<br />
===FULLPLAN===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|FULLPLAN [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Attaches the file path, line and column of the expressions in the original query string to the query plan. Values (items and sequences) have no input information attached.<br />
|}<br />
<br />
=Other Options=<br />
<br />
===AUTOFLUSH===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|AUTOFLUSH [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|true}}<br />
|-<br />
| '''Summary'''<br />
|Flushes database buffers to disk after each update. If this option is set to {{Code|false}}, bulk operations (multiple single updates) will be evaluated faster. As a drawback, the chance of data loss increases if the database is not explicitly flushed via the {{Command|FLUSH}} command.<br />
|}<br />
<br />
===WRITEBACK===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|WRITEBACK [boolean]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|false}}<br />
|-<br />
| '''Summary'''<br />
|Propagates updates on main-memory instances of files that have been retrieved via [[Databases#XML Documents|fn:doc]] and [[Databases#XML Documents|fn:collection]] back to disk:<br />
* This option can also be activated on [[Command-Line Options#BaseX Standalone|command line]] via <code>-u</code>.<br />
* Please take in mind that no backup will be created from your original files.<br />
* The serialization options can be controlled via the {{Option|EXPORTER}} option.<br />
|}<br />
<br />
===MAXSTAT===<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signature'''<br />
|{{Code|MAXSTAT [num]}}<br />
|-<br />
| '''Default'''<br />
|{{Code|30}}<br />
|-<br />
| '''Summary'''<br />
|Specifies the maximum number of index occurrences printed by the {{Command|INFO INDEX}} command.<br />
|}<br />
<br />
=Changelog=<br />
<br />
;Version 9.6<br />
* Added: {{Option|UNROLLLIMIT}}<br />
<br />
;Version 9.5<br />
* Updated: {{Option|INLINELIMIT}}: default reduced to 50.<br />
* Updated: {{Option|RESTXQERRORS}}: additionally suppress stack trace in HTTP response<br />
<br />
;Version 9.4<br />
* Added: {{Option|LOGTRACE}}<br />
<br />
;Version 9.3<br />
* Added: {{Option|WITHDB}}, {{Option|GZIP}}<br />
<br />
;Version 9.2<br />
* Added: {{Option|RESTXQERRORS}}, {{Option|FULLPLAN}}<br />
* Removed: <code>DOTPLAN</code>, <code>DOTCOMPACT</code><br />
<br />
;Version 9.0<br />
* Added: {{Option|ENFORCEINDEX}}, {{Option|COPYNODE}}, {{Option|IGNOREHOSTNAME}}<br />
<br />
;Version 8.6<br />
* Added: {{Option|FAIRLOCK}}, {{Option|PARSERESTXQ}}<br />
* Removed: {{Code|GLOBALLOCK}} (exclusive use of database lock)<br />
* Removed: {{Code|QUERYPATH}} (will now be internally assigned)<br />
* Removed: {{Code|CACHERESTXQ}} (replaced with PARSERESTXQ)<br />
<br />
;Version 8.5<br />
* Added: {{Option|CACHETIMEOUT}}, {{Option|LOGPATH}}<br />
* Updated: {{Option|AUTHMETHOD}}: {{Code|custom}} value added.<br />
<br />
;Version 8.4<br />
* Added: {{Option|TOKENINDEX}}, {{Option|TOKENINCLUDE}}<br />
* Added: {{Option|SPLITSIZE}} (replacing <code>INDEXSPLITSIZE</code> and <code>FTINDEXSPLITSIZE</code>)<br />
* Removed: <code>INDEXSPLITSIZE</code>, <code>FTINDEXSPLITSIZE</code><br />
<br />
;Version 8.3<br />
* Added: {{Option|CACHERESTXQ}}, {{Option|TEXTINCLUDE}}, {{Option|ATTRINCLUDE}}, {{Option|FTINCLUDE}}, {{Option|ARCHIVENAME}}<br />
<br />
;Version 8.2<br />
* Removed: <code>EVENTPORT</code>, <code>CACHEQUERY</code><br />
<br />
;Version 8.1<br />
* Added: {{Option|IGNORECERT}}, {{Option|RESTPATH}}<br />
<br />
;Version 8.0<br />
* Added: {{Option|MIXUPDATES}}, {{Option|AUTOOPTIMIZE}}, {{Option|AUTHMETHOD}}, {{Option|XINCLUDE}}<br />
* Updated: {{Option|PROXYPORT}}: default set to 0; will be ignored. {{Option|PROXYHOST}}, {{Option|NONPROXYHOSTS}}: empty strings will be ignored.<br />
<br />
;Version 7.8.1<br />
* Updated: {{Option|ADDARCHIVES}}: parsing of TAR and TGZ files.<br />
<br />
;Version 7.8<br />
<br />
* Added: {{Option|CSVPARSER}}, {{Option|JSONPARSER}}, {{Option|TEXTPARSER}}, {{Option|HTMLPARSER}}, {{Option|INLINELIMIT}}, {{Option|TAILCALLS}}, {{Option|DEFAULTDB}}, {{Option|RUNQUERY}}<br />
* Updated: {{Option|WRITEBACK}} only applies to main-memory document instances.<br />
* Updated: {{Option|DEBUG}} option can be changed at runtime by users with admin permissions.<br />
* Updated: default of {{Option|INTPARSE}} is now {{Code|false}}.<br />
* Removed: <code>HTMLOPT</code> (replaced with {{Option|HTMLPARSER}}), <code>PARSEROPT</code> (replaced with parser-specific options), <code>DOTDISPLAY</code>, <code>DOTTY</code><br />
<br />
;Version 7.7<br />
* Added: {{Option|ADDCACHE}}, {{Option|CHECKSTRINGS}}, {{Option|FTINDEXSPLITSIZE}}, {{Option|INDEXSPLITSIZE}}<br />
<br />
;Version 7.6<br />
* Added: {{Option|GLOBALLOCK}}<br />
* Added: store local options in configuration file after {{Code|# Local Options}} comments.<br />
<br />
;Version 7.5<br />
* Added: options can now be set via system properties<br />
* Added: a pragma expression can be used to locally change database options<br />
* Added: {{Option|USER}}, {{Option|PASSWORD}}, {{Option|LOG}}, {{Option|LOGMSGMAXLEN}}, {{Option|WEBPATH}}, {{Option|RESTXQPATH}}{{Option|HTTPLOCAL}}, {{Option|CREATEONLY}}, {{Option|STRIPNS}}<br />
* Removed: {{Code|HTTPPATH}}; {{Code|HTTPPORT}}: {{Code|jetty.xml}} configuration file is used instead<br />
* Removed: global options cannot be changed anymore during the lifetime of a BaseX instance<br />
<br />
;Version 7.3<br />
* Updated: {{Option|KEEPALIVE}}, {{Option|TIMEOUT}}: default values changed<br />
* Removed: {{Code|WILDCARDS}}; new index supports both fuzzy and wildcard queries<br />
* Removed: {{Code|SCORING}}; new scoring model will focus on lengths of text nodes and match options<br />
<br />
;Version 7.2<br />
* Added: {{Option|PROXYHOST}}, {{Option|PROXYPORT}}, {{Option|NONPROXYHOSTS}}, {{Option|HTMLOPT}}<br />
* Updated: {{Option|TIMEOUT}}: ignore timeout for admin users<br />
<br />
;Version 7.1<br />
* Added: {{Option|ADDRAW}}, {{Option|MAXLEN}}, {{Option|MAXCATS}}, {{Option|UPDINDEX}}<br />
* Updated: {{Option|BINDINGS}}<br />
<br />
;Version 7.0<br />
* Added: {{Option|SERVERHOST}}, {{Option|KEEPALIVE}}, {{Option|AUTOFLUSH}}, {{Option|QUERYPATH}}</div>Marco Letterehttps://docs.basex.org/index.php?title=Higher-Order_Functions_Module&diff=13240Higher-Order Functions Module2017-06-17T08:01:43Z<p>Marco Lettere: Added an example to hof:until to explain pre-fixed predicate evaluation</p>
<hr />
<div>This [[Module Library|XQuery Module]] adds some useful higher-order functions, additional to the [[Higher-Order Functions]] provided by the official specification.<br />
<br />
=Conventions=<br />
<br />
All functions in this module are assigned to the <code><nowiki>http://basex.org/modules/hof</nowiki></code> namespace, which is statically bound to the {{Code|hof}} prefix.<br/><br />
<br />
=Functions=<br />
<br />
==hof:id==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:id|$expr as item()*|item()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|hof:id(1 to 5)}} returns {{Code|1 2 3 4 5}}<br />
* With higher-order functions:<br />
<pre class="brush:xquery"><br />
let $sort := sort(?, hof:id#1)<br />
let $reverse-sort := sort(?, function($x) { -$x })<br />
return (<br />
$sort((1, 5, 3, 2, 4)),<br />
'|',<br />
$reverse-sort((1, 5, 3, 2, 4))<br />
)<br />
</pre><br />
returns: <code>1 2 3 4 5 | 5 4 3 2 1</code><br />
|}<br />
<br />
==hof:const==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:const|$expr as item()*, $ignored as item()*|item()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns its first argument unchanged and ignores the second. This function isn't useful on its own, but can be used as argument to other higher-order functions, e.g. when a function combining two values is expected and one only wants to retain the left one.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|hof:const(42, 1337)}} returns {{Code|42}}.<br />
* With higher-order functions:<br />
<pre class="brush:xquery"><br />
let $zip-sum := function($f, $seq1, $seq2) {<br />
sum(for-each-pair($seq1, $seq2, $f))<br />
}<br />
let $sum-all := $zip-sum(function($a, $b) { $a + $b }, ?, ?)<br />
let $sum-left := $zip-sum(hof:const#2, ?, ?)<br />
return (<br />
$sum-all((1, 1, 1, 1, 1), 1 to 5),<br />
$sum-left((1, 1, 1, 1, 1), 1 to 5)<br />
)<br />
</pre><br />
* Another use-case: When inserting a key into a map, {{Code|$f}} decides how to combine the new value with a possibly existing old one. {{Code|hof:const}} here means ignoring the old value, so that's normal insertion.<br />
<pre class="brush:xquery"><br />
let $insert-with := function($f, $map, $k, $v) {<br />
let $old := $map($k)<br />
let $new := if($old) then $f($v, $old) else $v<br />
return map:merge(($map, map:entry($k, $new)))<br />
}<br />
let $map := map { 'foo': 1 }<br />
let $add := $insert-with(function($a, $b) { $a + $b }, ?, ?, ?)<br />
let $ins := $insert-with(hof:const#2, ?, ?, ?)<br />
return (<br />
$add($map, 'foo', 2)('foo'),<br />
$ins($map, 'foo', 42)('foo')<br />
)<br />
</pre><br />
returns {{Code|3 42}}<br />
|}<br />
<br />
==hof:fold-left1==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:fold-left1|$seq as item()+, $f as function(item()*, item()) as item()*|item()*}}<br />
|-<br />
| '''Summary'''<br />
|Works the same as [[Higher-Order Functions#fn:fold-left|fn:fold-left]], but doesn't need a seed, because the sequence must be non-empty.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|hof:fold-left1(1 to 10, function($a, $b) { $a + $b })}} returns {{Code|55}}.<br />
* {{Code|hof:fold-left1((), function($a, $b) { $a + $b })}} throws {{Code|XPTY0004}}, because {{Code|$seq}} has to be non-empty.<br />
|}<br />
<br />
==hof:until==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:until|$pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*|item()*}}<br />
|-<br />
| '''Summary'''<br />
|Applies the function {{Code|$f}} to the initial value {{Code|$start}} until the predicate {{Code|$pred}} applied to the result returns {{Code|true()}}.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1)}} returns {{Code|1024}}.<br />
* Calculating the square-root of a number by iteratively improving an initial guess:<br />
<pre class="brush:xquery"><br />
let $sqrt := function($x as xs:double) as xs:double {<br />
hof:until(<br />
function($res) { abs($res * $res - $x) < 0.00001 },<br />
function($guess) { ($guess + $x div $guess) div 2 },<br />
$x<br />
)<br />
}<br />
return $sqrt(25)<br />
</pre><br />
returns {{Code|5.000000000053722}}.<br />
<br />
* The evaluation of the predicate is pre-fixed thus every cycle starts with the evaluation of the predicate first.<br />
<pre class="brush:xquery"><br />
declare function local:pred($res){ true() };<br />
declare function local:loop($val){ $val + 1 };<br />
<br />
hof:until(local:pred#1, local:loop#1, 0)<br />
</pre><br />
returns {{Code|0}}.<br />
<br />
|}<br />
<br />
==hof:scan-left==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:scan-left|$seq as item()*, $start as item()*, $f as function(item()*, item()) as item()*|item()*}}<br />
|-<br />
| '''Summary'''<br />
|This function is similar to [[Higher-Order Functions#fn:fold-left|fn:fold-left]], but it returns a list of successive reduced values from the left. It is equivalent to:<br />
<pre class="brush:xquery"><br />
declare function hof:scan-left($seq, $acc, $f) {<br />
if(empty($seq)) then $acc else (<br />
$acc,<br />
hof:scan-left(tail($seq), $f($acc, head($seq)), $f)<br />
)<br />
};<br />
</pre><br />
|-<br />
| '''Examples'''<br />
|<br />
* Returns triangular numbers:<br />
<pre class="brush:xquery"><br />
hof:scan-left(1 to 10, 0, function($a, $b) { $a + $b })<br />
</pre><br />
|}<br />
<br />
==hof:take-while==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:take-while|$seq as item()*, $pred as function(item()) as xs:boolean|item()*}}<br />
|-<br />
| '''Summary'''<br />
|The function returns items of <code>$seq</code> as long as the predicate <code>$pred</code> is satisfied. It is equivalent to:<br />
<pre class="brush:xquery"><br />
declare function hof:take-while($seq, $pred) {<br />
if(empty($seq) or not($pred(head($seq)))) then () else (<br />
head($seq),<br />
hof:take-while(tail($seq), $pred)<br />
)<br />
};<br />
</pre><br />
|-<br />
| '''Examples'''<br />
|<br />
* Computes at most 100 random integers, but stops if an integer is smaller than 10:<br />
<pre class="brush:xquery"><br />
hof:take-while(<br />
(1 to 100) ! random:integer(50),<br />
function($x) { $x >= 10 }<br />
)<br />
</pre><br />
|}<br />
<br />
==hof:top-k-by==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:top-k-by|$seq as item()*, $sort-key as function(item()) as item(), $k as xs:integer|item()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns the {{Code|$k}} items in {{Code|$seq}} that are greatest when sorted by the result of {{Code|$f}} applied to the item. The function is a much more efficient implementation of the following scheme:<br />
<pre class="brush:xquery"><br />
(for $x in $seq<br />
order by $sort-key($x) descending<br />
return $x<br />
)[position() <= $k]<br />
</pre><br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|hof:top-k-by(1 to 1000, hof:id#1, 5)}} returns {{Code|1000 999 998 997 996}}<br />
* {{Code|hof:top-k-by(1 to 1000, function($x) { -$x }, 3)}} returns {{Code|1 2 3}}<br />
* <code>hof:top-k-by(<x a='1' b='2' c='3'/>/@*, xs:integer#1, 2)/node-name()</code> returns {{Code|c b}}<br />
|}<br />
<br />
==hof:top-k-with==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|hof:top-k-with|$seq as item()*, $lt as function(item(), item()) as xs:boolean, $k as xs:integer|item()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns the {{Code|$k}} items in {{Code|$seq}} that are greatest when sorted in the order of the ''less-than'' predicate {{Code|$lt}}. The function is a general version of {{Code|hof:top-k-by($seq, $sort-key, $k)}}.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|hof:top-k-with(1 to 1000, function($a, $b) { $a lt $b }, 5)}} returns {{Code|1000 999 998 997 996}}<br />
* {{Code|hof:top-k-with(-5 to 5, function($a, $b) { abs($a) gt abs($b) }, 5)}} returns {{Code|0 1 -1 2 -2}}<br />
|}<br />
<br />
=Changelog=<br />
<br />
;Version 8.1<br />
<br />
* Added: [[#hof:scan-left|hof:scan-left]], [[#hof:take-while|hof:take-while]]<br />
<br />
;Version 7.2<br />
<br />
* Added: [[#hof:top-k-by|hof:top-k-by]], [[#hof:top-k-with|hof:top-k-with]]<br />
* Removed: hof:iterate<br />
<br />
;Version 7.0<br />
<br />
* module added</div>Marco Letterehttps://docs.basex.org/index.php?title=JSON_Module&diff=12076JSON Module2015-11-13T09:45:08Z<p>Marco Lettere: Fixed signature of json:serialize</p>
<hr />
<div>This [[Module Library|XQuery Module]] contains functions to parse and serialize JSON documents. [http://www.json.org/ JSON (JavaScript Object Notation)] is a popular data exchange format for applications written in JavaScript. As there are notable differences between JSON and XML, no mapping exists that guarantees a lossless, bidirectional conversion between JSON and XML. For this reason, we offer various mappings, all of which are suited to different use cases.<br />
<br />
=Conventions=<br />
<br />
All functions in this module are assigned to the <code><nowiki>http://basex.org/modules/json</nowiki></code> namespace, which is statically bound to the {{Code|json}} prefix.<br/><br />
All errors are assigned to the <code><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.<br />
<br />
==Conversion Formats==<br />
<br />
===Direct===<br />
<br />
The {{Code|direct}} conversion format allows a lossless conversion from JSON to XML and back. The transformation is based on the following rules:<br />
<br />
* The resulting document has a {{Code|<json>}} root node. <br />
* Object pairs are represented via elements. The name of a pair is rewritten to an element name:<br />
** Empty names are represented by a single underscore ({{Code|_}}). Existing underscores are rewritten to two underscores ({{Code|__}}), and characters that are not valid in element names are rewritten to an underscore and the character’s four-digit Unicode.<br />
** If the {{Code|lax}} option is set to {{Code|true}}, invalid characters are simply replaced with underscores or (when invalid as first character of an element name) prefixed with an underscore. The resulting names are better readable, but cannot always be converted back to their original form.<br />
* Array entries are also represented via elements. {{Code|_}} is used as element name.<br />
* Object and array values are stored in text nodes.<br />
* The types of values are represented via {{Code|type}} attributes:<br />
** The existing types are ''string'', ''number'', ''boolean'', ''null'', ''object'', and ''array''.<br />
** As most values are strings, the ''string'' type is by default omitted.<br />
<br />
===Attributes===<br />
<br />
The {{Code|attributes}} format is lossless, too. The transformation based on the following rules:<br />
<br />
* The resulting document has a {{Code|<json>}} root node. <br />
* Object pairs are represented via {{Code|<pair>}} elements. The name of a pair is stored in a {{Code|name}} attribute.<br />
* Array entries are represented via {{Code|<item>}} elements.<br />
* Object and array values are stored in text nodes.<br />
* The types of values are represented via {{Code|type}} attributes:<br />
** The existing types are ''string'', ''number'', ''boolean'', ''null'', ''object'', and ''array''.<br />
** As most values are strings, the ''string'' type is by default omitted.<br />
<br />
===Map===<br />
<br />
The {{Code|map}} format is lossless, too. It converts a JSON document to an XQuery map and vice versa. The conversion rules are the same as for [[XQuery 3.1#fn:parse-json|fn:parse-json]].<br />
<br />
===Basic===<br />
<br />
The {{Code|basic}} format is another lossless format. It converts a JSON document to an XML node and vice versa. The conversion rules are the same as for [[XQuery 3.1#fn:json-to-xml|fn:json-to-xml]].<br />
<br />
===JsonML===<br />
<br />
The {{Code|jsonml}} format is designed to convert XML to JSON and back, using the JsonML dialect. JsonML allows the transformation of arbitrary XML documents, but namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official [http://jsonml.org/XML JsonML documentation].<br />
<br />
'''A little advice''': in the Database Creation dialog of the GUI, if you select JSON Parsing and switch to the ''Parsing'' tab, you can see the effects of some of the conversion options.<br />
<br />
==Options==<br />
<br />
{{Mark|Updated with Version 8.4}}: <code>unescape</code> changed to <code>escape</code>.<br />
<br />
The following options are available (the ''Direction'' column indicates if an option applies to parsing, serialization, or both operations):<br />
<br />
{| class="wikitable sortable" width="100%"<br />
|- valign="top"<br />
! width="140" | Option<br />
! width="50%" | Description<br />
! Allowed<br />
! Default<br />
! Direction<br />
|- valign="top"<br />
| {{Code|format}}<br />
| Specifies the format for converting JSON data.<br />
| {{Code|direct}}, {{Code|attributes}}, {{Code|jsonml}}, {{Code|map}}<br />
| {{Code|direct}}<br />
| ''parse'', ''serialize''<br />
|- valign="top"<br />
| {{Code|liberal}}<br />
| Determines if minor deviations from [http://www.rfc-editor.org/rfc/rfc7159.txt RFC 7159] will be ignored.<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|no}}<br />
| ''parse''<br />
|- valign="top"<br />
| {{Code|merge}}<br />
| This option is considered when {{Code|direct}} or {{Code|attributes}} conversion is used:<br/><br />
* If a name has the same type throughout the document, the {{Code|type}} attribute will be omitted. Instead, the name will be listed in additional, type-specific attributes in the root node<br />
* The attributes are named by their type in plural (''numbers'', ''booleans'', ''nulls'', ''objects'' and ''arrays''), and the attribute value contains all names with that type, separated by whitespaces.<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|no}}<br />
| ''parse'', ''serialize''<br />
|- valign="top"<br />
| {{Code|strings}}<br />
| Indicates if {{Code|type}} attributes will be added for strings.<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|yes}}<br />
| ''parse'', ''serialize''<br />
|- valign="top"<br />
| {{Code|lax}}<br />
| Specifies if a lax approach is used to convert QNames to JSON names.<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|no}}<br />
| ''parse'', ''serialize''<br />
|- valign="top"<br />
| {{Code|escape}}<br />
| Indicates if escaped characters are expanded (for example, {{Code|\n}} becomes a single {{Code|x0A}} character, while {{Code|\u20AC}} becomes the character {{Code|€}}).<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|yes}}<br />
| ''parse''<br />
|- valign="top"<br />
| {{Code|escape}}<br />
| Indicates if characters are escaped whenever the JSON syntax requires it. This option can be set to {{Code|no}} if strings are already in escaped form and no further escaping is permitted.<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|yes}}<br />
| ''serialize''<br />
|- valign="top"<br />
| {{Code|indent}}<br />
| Indicates if whitespace should be added to the output with the aim of improving human legibility. If the parameter is set as in the query prolog, it overrides the {{Code|indent}} [[Serialization|serialization parameter]].<br />
| {{Code|yes}}, {{Code|no}}<br />
| {{Code|yes}}<br />
| ''serialize''<br />
|}<br />
<br />
=Functions=<br />
<br />
==json:parse==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|json:parse|$input as xs:string|element(json)}}<br/>{{Func|json:parse|$input as xs:string, $options as map(xs:string, xs:string)|item()}}<br />
|-<br />
| '''Summary'''<br />
|Converts the JSON document specified by {{Code|$input}} to an XML document or a map. If the input can be successfully parsed, it can be serialized back to the original JSON representation. The {{Code|$options}} argument can be used to control the way the input is converted.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXJS0001|#Errors}} the specified input cannot be parsed as JSON document.<br />
|}<br />
<br />
==json:serialize==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|json:serialize|$input as item()?|xs:string}}<br/>{{Func|json:serialize|$input as item()?, $options as map(xs:string, xs:string)|xs:string}}<br />
|-<br />
| '''Summary'''<br />
|Serializes the node specified by {{Code|$input}} as JSON, and returns the result as {{Code|xs:string}} instance. The node is expected to conform to the output created by the [[#json:parse|json:parse()]] function. All other items will be serialized as specified for the [[XQuery 3.1#JSON Serialization|json output method]] of the official specification.<br />Items can also be serialized as JSON if the [[Serialization|Serialization Parameter]] {{Code|method}} is set to {{Code|json}}.<br/>The {{Code|$options}} argument can be used to control the way the input is serialized.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXJS0002|#Errors}} the specified node cannot be serialized as JSON document.<br />
|}<br />
<br />
=Examples=<br />
<br />
==BaseX Format==<br />
<br />
'''Example 1: Adds all JSON documents in a directory to a database'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
let $database := "database"<br />
for $name in file:list('.', false(), '*.json')<br />
let $file := file:read-text($name)<br />
let $json := json:parse($file)<br />
return db:add($database, $json, $name) <br />
</pre><br />
<br />
'''Example 2: Converts a simple JSON string to XML and back'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
json:parse('{}')<br />
</pre><br />
<br />
'''Result:'''<br />
<pre class="brush:xml"><br />
<json type="object"/><br />
</pre><br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
(: serialize result as plain text :)<br />
declare option output:method 'text';<br />
json:serialize(<json type="object"/>)<br />
</pre><br />
<br />
'''Result:'''<br />
<pre class="brush:xquery"><br />
{ }<br />
</pre><br />
<br />
'''Example 3: Converts a JSON string with simple objects and arrays'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
json:parse('{<br />
"title": "Talk On Travel Pool",<br />
"link": "http://www.flickr.com/groups/talkontravel/pool/",<br />
"description": "Travel and vacation photos from around the world.",<br />
"modified": "2014-02-02T11:10:27Z",<br />
"generator": "http://www.flickr.com/"<br />
}')<br />
</pre><br />
<br />
'''Result:'''<br />
<pre class="brush:xml"><br />
<json type="object"><br />
<title>Talk On Travel Pool</title><br />
<link>http://www.flickr.com/groups/talkontravel/pool/</link><br />
<description>Travel and vacation photos from around the world.</description><br />
<modified>2014-02-02T11:10:27Z</modified><br />
<generator>http://www.flickr.com/</generator><br />
</json><br />
</pre><br />
<br />
'''Example 4: Converts a JSON string with different data types'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
let $options := map { 'merge': true() }<br />
return json:parse('{<br />
"first_name": "John",<br />
"last_name": "Smith",<br />
"age": 25,<br />
"address": {<br />
"street": "21 2nd Street",<br />
"city": "New York",<br />
"code": 10021<br />
},<br />
"phone": [<br />
{<br />
"type": "home",<br />
"number": "212 555-1234"<br />
},<br />
{<br />
"type": "mobile",<br />
"number": 1327724623<br />
}<br />
]<br />
}', $options)<br />
</pre><br />
<br />
'''Result:'''<br />
<pre class="brush:xml"><br />
<json numbers="age code" arrays="phone" objects="json address value"><br />
<first__name>John</first__name><br />
<last__name>Smith</last__name><br />
<age>25</age><br />
<address><br />
<street>21 2nd Street</street><br />
<city>New York</city><br />
<code>10021</code><br />
</address><br />
<phone><br />
<_><br />
<type>home</type><br />
<number>212 555-1234</number><br />
</_><br />
<_><br />
<type>mobile</type><br />
<number type="number">1327724623</number><br />
</_><br />
</phone><br />
</json><br />
</pre><br />
<br />
==JsonML Format==<br />
<br />
'''Example 1: Converts all XML documents in a database to JsonML and writes them to disk'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
for $doc in collection('json')<br />
let $name := document-uri($doc)<br />
let $json := json:serialize($doc)<br />
return file:write($name, $json)<br />
</pre><br />
<br />
'''Example 2: Converts a simple XML fragment to the JsonML format'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
json:serialize(<xml/>, map { 'format': 'jsonml' })<br />
</pre><br />
<br />
'''Result:'''<br />
<pre><br />
["xml"]<br />
</pre><br />
<br />
'''Example 3: Converts an XML document with elements and text'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })<br />
</pre><br />
<br />
'''flickr.xml:'''<br />
<pre class="brush:xml"><br />
<flickr><br />
<title>Talk On Travel Pool</title><br />
<link>http://www.flickr.com/groups/talkontravel/pool/</link><br />
<description>Travel and vacation photos from around the world.</description><br />
<modified>2014-02-02T11:10:27Z</modified><br />
<generator>http://www.flickr.com/</generator><br />
</flickr><br />
</pre><br />
<br />
'''Result:'''<br />
<pre><br />
["flickr",<br />
["title",<br />
"Talk On Travel Pool"],<br />
["link",<br />
"http://www.flickr.com/groups/talkontravel/pool/"],<br />
["description",<br />
"Travel and vacation photos from around the world."],<br />
["modified",<br />
"2014-02-02T11:10:27Z"],<br />
["generator",<br />
"http://www.flickr.com/"]]<br />
</pre><br />
<br />
'''Example 4: Converts a document with nested elements and attributes'''<br />
<br />
'''Query:'''<br />
<pre class="brush:xquery"><br />
json:serialize(doc('input.xml'), map { 'format': 'jsonml' })<br />
</pre><br />
<br />
'''input.xml:'''<br />
<pre class="brush:xml"><br />
<address id='1'><br />
<!-- comments will be discarded --><br />
<last_name>Smith</last_name><br />
<age>25</age><br />
<address xmlns='will be dropped as well'><br />
<street>21 2nd Street</street><br />
<city>New York</city><br />
<code>10021</code><br />
</address><br />
<phone type='home'>212 555-1234</phone><br />
</address><br />
</pre><br />
<br />
'''Result:'''<br />
<pre><br />
["address", {"id":"1"},<br />
["last_name",<br />
"Smith"],<br />
["age",<br />
"25"],<br />
["address",<br />
["street",<br />
"21 2nd Street"],<br />
["city",<br />
"New York"],<br />
["code",<br />
"10021"]],<br />
["phone", {"type":"home"},<br />
"212 555-1234"]]<br />
</pre><br />
<br />
=Errors=<br />
<br />
{| class="wikitable" width="100%"<br />
! width="110"|Code<br />
|Description<br />
|-<br />
|{{Code|BXJS0001}}<br />
|The specified input cannot be parsed as JSON document.<br />
|-<br />
|{{Code|BXJS0002}}<br />
|The specified node cannot be serialized as JSON document.<br />
|}<br />
<br />
=Changelog=<br />
<br />
;Version 8.4<br />
* Updated: <code>unescape</code> changed to <code>escape</code>.<br />
<br />
;Version 8.2<br />
* Added: Conversion format <code>[[#Basic|basic]]</code>.<br />
<br />
;Version 8.0<br />
* Updated: Serialization aligned with the {{Code|json}} output method of the official specification.<br />
* Added: {{Code|liberal}} option.<br />
* Removed: {{Code|spec}} option.<br />
<br />
;Version 7.8<br />
* Removed: {{Code|json:parse-ml}}, {{Code|json:serialize-ml}}.<br />
* Updated: {{Code|json:parse}} now returns a document node instead of an element, or an XQuery map if {{Code|format}} is set to {{Code|.map}}.<br />
<br />
;Version 7.7.2<br />
* Updated: {{Code|$options}} argument added to [[#json:parse|json:parse]] and [[#json:serialize|json:serialize]].<br />
* Updated: [[#json:parse-ml|json:parse-ml]] and [[#json:serialize-ml|json:serialize-ml]] are now ''deprecated''.<br />
<br />
The module was introduced with Version 7.0.<br />
<br />
[[Category:XQuery]]</div>Marco Letterehttps://docs.basex.org/index.php?title=Database_Module&diff=11853Database Module2015-07-02T09:25:46Z<p>Marco Lettere: </p>
<hr />
<div>This [[Module Library|XQuery Module]] contains functions for processing databases from within XQuery. Existing databases can be opened and listed, its contents can be directly accessed, documents can be added to and removed, etc.<br />
<br />
=Conventions=<br />
<br />
All functions in this module are assigned to the <code><nowiki>http://basex.org/modules/db</nowiki></code> namespace, which is statically bound to the {{Code|db}} prefix.<br/><br />
All errors are assigned to the <code><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.<br />
<br />
==Database Nodes==<br />
<br />
Database nodes are XML nodes which are either stored in a persistent database or part of a so-called ''database fragment''. All XML fragments can be converted to database fragments by e. g. applying the [[XQuery_Update#transform|transform]] expression on an XML fragment:<br />
<br />
<pre class="brush:xquery"><br />
copy $c := element hello { 'world' } modify () return $c<br />
</pre><br />
<br />
=General Functions=<br />
<br />
==db:system==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:system||element(system)}}<br />
|-<br />
| '''Summary'''<br />
|Returns information on the database system, such as the database path and current database settings. The output is similar to the [[Commands#INFO|INFO]] command.<br />
|}<br />
<br />
==db:info==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:info|$db as xs:string|element(database)}}<br />
|-<br />
| '''Summary'''<br />
|Returns meta information on the database {{Code|$db}}. The output is similar to the [[Commands#INFO DB|INFO DB]] command.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|}<br />
<br />
==db:list==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:list||xs:string*}}<br/>{{Func|db:list|$db as xs:string|xs:string*}}<br/>{{Func|db:list|$db as xs:string, $path as xs:string|xs:string*}}<br />
|-<br />
| '''Summary'''<br />
|Returns a string sequence with the names of all databases:<br />
* If a database {{Code|$db}} is specified, all documents and raw files of the specified database are returned.<br />
* The list of resources can be further restricted by the {{Code|$path}} argument.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:list("docs")}} returns the names of all documents from the database named {{Code|docs}}.<br />
|}<br />
<br />
==db:list-details==<br />
<br />
{{Mark|Updated with Version 8.3}}: also return size (number of nodes) of document resources<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:list-details||element(database)*}}<br/>{{Func|db:list-details|$db as xs:string|element(resource)*}}<br/>{{Func|db:list-details|$db as xs:string, $path as xs:string|element(resource)*}}<br />
|-<br />
| '''Summary'''<br />
|<br />
* If no argument is specified, a sequence of elements is returned. A single element contains the name of a database, the number of stored resources, the date of modification, and the database path.<br />
* If {{Code|$db}} is specified, a sequence of elements is returned, comprising information on all resources of the addressed database. An element contains the name of the resource, the content type, the modified date, the raw flag and the size of a resource.<br />
* For binary resources, the size is the number of bytes; for document resources, the size is the number of nodes<br />
* Returned databases resources can be further restricted by the {{Code|$path}} argument.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:list-details("docs")}} returns the names plus additional data of all documents from the database named {{Code|docs}}.<br />
|}<br />
<br />
==db:backups==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:backups||element(backup)*}}<br/>{{Func|db:backups|$db as xs:string|element(backup)*}}<br />
|-<br />
| '''Summary'''<br />
|Returns an element sequence containing all available database backups.<br/>If a database {{Code|$db}} is specified, the sequence will be restricted to the backups matching this database.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:backups("factbook")}} returns all backups that have been made from the {{Code|factbook}} database.<br />
|}<br />
<br />
=Read Operations=<br />
<br />
==db:open==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:open|$db as xs:string|document-node()*}}<br />{{Func|db:open|$db as xs:string, $path as xs:string|document-node()*}}<br />
|-<br />
| '''Summary'''<br />
|Opens the database {{Code|$db}} and returns all document nodes.<br/>The document nodes to be returned can be filtered with the {{Code|$path}} argument.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:open("docs")}} returns all documents from the database named {{Code|docs}}.<br />
* {{Code|db:open("db", "one")}} returns all documents from the database named {{Code|db}} located in the path {{Code|one}}.<br />
* <code>for $i in 1 to 3 return db:open("db" || $i)//item</code> returns all item elements from the databases {{Code|db1}}, {{Code|db2}} and {{Code|db3}}.<br />
|}<br />
<br />
==db:open-pre==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:open-pre|$db as xs:string, $pre as xs:integer|node()}}<br />
|-<br />
| '''Summary'''<br />
|Opens the database {{Code|$db}} and returns the node with the specified {{Code|$pre}} value.<br/>The [[Node Storage#PRE Value|PRE value]] provides very fast access to an existing database node, but it will change whenever a node with a smaller ''pre'' values is added to or deleted from a database.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0009|XQuery Errors#BaseX Errors}} the specified pre value does not exist in the database.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:open-pre("docs", 0)}} returns the first database node from the database named {{Code|docs}}.<br />
|}<br />
<br />
==db:open-id==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:open-id|$db as xs:string, $id as xs:integer|node()}}<br />
|-<br />
| '''Summary'''<br />
|Opens the database {{Code|$db}} and returns the node with the specified {{Code|$id}} value.<br />Each database node has a ''persistent'' [[Node Storage#ID Value|ID value]]. Access to the node id can be sped up by turning on the [[Options#UPDINDEX|UPDINDEX]] option.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0009|XQuery Errors#BaseX Errors}} the specified id value does not exist in the database.<br />
|}<br />
<br />
==db:node-pre==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:node-pre|$nodes as node()*|xs:integer*}}<br />
|-<br />
| '''Summary'''<br />
|Returns the ''pre'' values of the nodes supplied by {{Code|$nodes}}, which must all be [[#Database Nodes|database nodes]].<br/>The [[Node Storage#PRE Value|PRE value]] provides very fast access to an existing database node, but it will change whenever a node with a smaller ''pre'' values is added to or deleted from a database.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0001|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:node-pre(doc("input"))}} returns {{Code|0}} if the database {{Code|input}} contains a single document.<br />
|}<br />
<br />
==db:node-id==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:node-id|$nodes as node()*|xs:integer*}}<br />
|-<br />
| '''Summary'''<br />
|Returns the ''id'' values of the nodes supplied by {{Code|$nodes}}, which must all be [[#Database Nodes|database nodes]].<br/>Each database node has a ''persistent'' [[Node Storage#ID Value|ID value]]. Access to the node id can be sped up by turning on the [[Options#UPDINDEX|UPDINDEX]] option.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0001|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.<br />
|}<br />
<br />
==db:retrieve==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:retrieve|$db as xs:string, $path as xs:string|xs:base64Binary}}<br />
|-<br />
| '''Summary'''<br />
|Returns a [[Binary Data|binary resource]] addressed by the database {{Code|$db}} and {{Code|$path}} as [[Streaming Module|streamable]] {{Code|xs:base64Binary}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0003|#Errors}} the database is not ''persistent'' (stored on disk).<br/>{{Error|FODC0002|XQuery Errors#Functions Errors}} the addressed resource cannot be retrieved.<br/>{{Error|FODC0007|XQuery Errors#Functions Errors}} the specified path is invalid.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|declare option output:method 'raw';<br/>db:retrieve("DB", "music/01.mp3")}} returns the specified audio file as raw data.<br />
* <code><nowiki>stream:materialize(db:retrieve("DB", "music/01.mp3"))</nowiki></code> returns a materialized representation of the streamable result.<br />
|}<br />
<br />
==db:export==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:export|$db as xs:string, $path as xs:string|empty-sequence()}}<br />{{Func|db:export|$db as xs:string, $path as xs:string, $params as item()|empty-sequence()}}<br /><br />
|-<br />
| '''Summary'''<br />
|Exports the specified database {{Code|$db}} to the specified file {{Code|$path}}. Existing files will be overwritten. The {{Code|$params}} argument contains serialization parameters (see [[Serialization]] for more details), which can either be specified<br /><br />
* as children of an {{Code|&lt;output:serialization-parameters/&gt;}} element, as defined for the [http://www.w3.org/TR/xpath-functions-30/#func-serialize fn:serialize()] function; e.g.:<br />
<pre class="brush:xml"><br />
<output:serialization-parameters><br />
<output:method value='xml'/><br />
<output:cdata-section-elements value="div"/><br />
...<br />
</output:serialization-parameters><br />
</pre><br />
* as map, which contains all key/value pairs:<br />
<pre class="brush:xml"><br />
map { "method": "xml", "cdata-section-elements": "div", ... }<br />
</pre><br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
| Export all files as text:<br/><br />
<pre class="brush:xquery"><br />
db:export("DB", "/home/john/xml/texts", map { 'method': 'text' })<br />
</pre><br />
The following query can be used to export parts of the database:<br />
<pre class="brush:xquery"><br />
let $target := '/home/john/xml/target'<br />
for $doc in db:open('DB', 'collection')<br />
let $path := $target || db:path($doc)<br />
return (<br />
file:create-dir(file:parent($path)),<br />
file:write($path, $doc)<br />
)<br />
</pre><br />
|}<br />
<br />
=Contents=<br />
<br />
==db:text==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:text|$db as xs:string, $string as item()|text()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns all text nodes of the database {{Code|$db}} that have {{Code|$string}} as their string value. If available, the value index is used to speed up evaluation.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:text("DB", "QUERY")/..}} returns the parents of all text nodes of the database {{Code|DB}} that match the string {{Code|QUERY}}.<br />
|}<br />
<br />
==db:text-range==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:text-range|$db as xs:string, $min as xs:string, $max as xs:string|text()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns all text nodes of the database {{Code|$db}} that are located in between the {{Code|$min}} and {{Code|$max}} strings. If available, the value index is used to speed up evaluation.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:text-range("DB", "2000", "2001")}} returns all text nodes of the database {{Code|DB}} that are found in between {{Code|2000}} and {{Code|2001}}.<br />
|}<br />
<br />
==db:attribute==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:attribute|$db as xs:string, $string as item()|attribute()*}}<br/>{{Func|db:attribute|$db as xs:string, $string as item(), $attname as xs:string|attribute()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns all attribute nodes of the database {{Code|$db}} that have {{Code|$string}} as string value. If available, the value index is used to speed up evaluation.<br />If {{Code|$attname}} is specified, the resulting attribute nodes are filtered by their attribute name.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:attribute("DB", "QUERY", "id")/..}} returns the parents of all {{Code|id}} attribute nodes of the database {{Code|DB}} that have {{Code|QUERY}} as string value.<br />
|}<br />
<br />
==db:attribute-range==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string|attribute()*}}<br/>{{Func|db:attribute-range|$db as xs:string, $min as xs:string, $max as xs:string, $attname as xs:string|attribute()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns all attributes of the database {{Code|$db}}, the string values of which are larger than or equal to {{Code|$min}} and smaller than or equal to {{Code|$max}}. If available, the value index is used to speed up evaluation.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:attribute-range("DB", "id456", "id473", 'id')}} returns all {{Code|@id}} attributes of the database {{Code|DB}} that have a string value in between {{Code|id456}} and {{Code|id473}}.<br />
|}<br />
<br />
=Updates=<br />
<br />
'''Important note:''' All functions in this section are ''updating functions'': they will not be immediately executed, but queued on the [[XQuery Update#Pending Update List|Pending Update List]], which will be processed after the actual query has been evaluated. This means that the order in which the functions are specified in the query does usually not reflect the order in which the code will be evaluated.<br />
<br />
==db:create==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:create|$db as xs:string|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*, $paths as xs:string*|empty-sequence()}}<br/>{{Func|db:create|$db as xs:string, $inputs as item()*, $paths as xs:string*, $options as map(xs:string, xs:string)|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Creates a new database with name {{Code|$db}} and adds initial documents specified via {{Code|$inputs}} to the specified {{Code|$paths}}. An existing database will be overwritten.<br/>{{Code|$inputs}} may be strings or nodes different than attributes. If the {{Code|$input}} source is not a file or a folder, the {{Code|$paths}} argument is mandatory.<br/>Please note that {{Code|db:create}} will be placed last on the [[XQuery Update#Pending Update List|Pending Update List]]. As a consequence, a newly created database cannot be addressed in the same query.<br/>The {{Code|$options}} argument can be used to change the indexing behavior. Allowed options are all [[Options#Parsing|parsing]], [[Options#XML Parsing|XML parsing]], [[Options#Indexing|indexing]] and [[Options#Full-Text|full-text]] options in lower case.<br />
|-<br />
| '''Errors'''<br />
|{{Error|FODC0002|XQuery Errors#Functions Errors}} {{Code|$inputs}} points to an unknown resource.<br/>{{Error|FOUP0001|XQuery Errors#Update Errors}} {{Code|$inputs}} is neither string nor a document node.<br/>{{Error|BXDB0007|#Errors}} {{Code|$db}} is opened by another process.<br/>{{Error|BXDB0011|#Errors}} {{Code|$db}} is not a [[Commands#Valid_Names|valid database name]].<br/>{{Error|BXDB0012|#Errors}} two {{Code|db:create}} statements with the same database name were specified.<br/>{{Error|BXDB0013|#Errors}} the number of specified inputs and paths differs.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:create("DB")}} creates the empty database {{Code|DB}}.<br />
* {{Code|db:create("DB", "/home/dir/doc.xml")}} creates the database {{Code|DB}} and adds the document {{Code|/home/dir/doc.xml}} as initial content.<br />
* {{Code|db:create("DB", <a/>, "doc.xml")}} creates the database {{Code|DB}} and adds the document with content {{Code|&lt;a/&gt;}} under the name {{Code|doc.xml}}.<br />
* {{Code|db:create("DB", "/home/dir/", "docs/dir")}} creates the database {{Code|DB}} and adds the documents in {{Code|/home/dir}} to the database under the path {{Code|docs/dir}}.<br />
* <code>db:create("DB", file:list('.'), (), map { 'ftindex': true() })</code> adds all files of the current working directory to a new database, preserving relative filesystem paths and creating a full-text index.<br />
|}<br />
<br />
==db:drop==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:drop|$db as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Drops the database {{Code|$db}} and all connected resources.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0007|#Errors}} {{Code|$db}} is opened by another process.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:drop("DB")}} drops the database {{Code|DB}}.<br />
|}<br />
<br />
==db:add==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:add|$db as xs:string, $input as item()|empty-sequence()}}<br/>{{Func|db:add|$db as xs:string, $input as item(), $path as xs:string|empty-sequence()}}<br/>{{Func|db:add|$db as xs:string, $input as item(), $path as xs:string, $options as map(xs:string, xs:string)|empty-sequence()|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Adds documents specified by {{Code|$input}} to the database {{Code|$db}} and the specified {{Code|$path}}. A document with the same path may occur more than once in a database. If this is unwanted, [[#db:replace|db:replace]] can be used.<br/>{{Code|$input}} may be a string or a node different than attribute. If the {{Code|$input}} source is not a file or a folder, {{Code|$path}} must be specified.<br/>The {{Code|$options}} argument can be used to control parsing. The syntax is identical to the [[#db:create|db:create]] function. Allowed options are all [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options in lower case.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|FODC0002|XQuery Errors#Functions Errors}} {{Code|$input}} points to an unknown resource.<br/>{{Error|FOUP0001|XQuery Errors#Update Errors}} {{Code|$input}} is neither string nor a document node.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:add("DB", "/home/dir/doc.xml")}} adds the file {{Code|/home/dir/doc.xml}} to the database {{Code|DB}}.<br />
* {{Code|db:add("DB", <a/>, "doc.xml")}} adds a document node to the database {{Code|DB}} under the name {{Code|doc.xml}}.<br />
* {{Code|db:add("DB", "/home/dir", "docs/dir")}} adds all documents in {{Code|/home/dir}} to the database {{Code|DB}} under the path {{Code|docs/dir}}.<br />
|}<br />
<br />
==db:delete==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:delete|$db as xs:string, $path as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Deletes document(s), specified by {{Code|$path}}, from the database {{Code|$db}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:delete("DB", "docs/dir/doc.xml")}} deletes the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}}.<br />
* {{Code|db:delete("DB", "docs/dir")}} deletes all documents with paths beginning with {{Code|docs/dir}} in the database {{Code|DB}}.<br />
|}<br />
<br />
==db:copy==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:copy|$db as xs:string, $newname as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Creates a copy of the database specified by {{Code|$db}} to {{Code|$newname}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0011|XQuery Errors#BaseX Errors}} Invalid database name.<br/>{{Error|BXDB0016|XQuery Errors#BaseX Errors}} Name of source and target database is equal.<br />
|}<br />
<br />
==db:alter==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:alter|$db as xs:string, $newname as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Renames the database specified by {{Code|$db}} to {{Code|$newname}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0011|XQuery Errors#BaseX Errors}} Invalid database name.<br/>{{Error|BXDB0016|XQuery Errors#BaseX Errors}} Name of source and target database is equal.<br />
|}<br />
<br />
==db:create-backup==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:create-backup|$db as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Creates a backup of the database {{Code|$db}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0011|XQuery Errors#BaseX Errors}} Invalid database name.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:create-backup("DB")}} creates a backup of the database {{Code|DB}}.<br />
|}<br />
<br />
==db:drop-backup==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:drop-backup|$name as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Drops all backups of the database with the specified {{Code|$name}}. If the given {{Code|$name}} points to a specific backup file, only this specific backup file is deleted.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} No backup file found.<br/>{{Error|BXDB0011|XQuery Errors#BaseX Errors}} Invalid database name.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:drop-backup("DB")}} drops all backups of the database {{Code|DB}}.<br />
* {{Code|db:drop-backup("DB-2014-03-13-17-36-44")}} drops the specific backup file {{Code|DB-2014-03-13-17-36-44.zip}} of the database {{Code|DB}}.<br />
|}<br />
<br />
==db:restore==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:restore|$name as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Restores the database with the specified {{Code|$name}}. The {{Code|$name}} may include the timestamp of the backup file.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0011|XQuery Errors#BaseX Errors}} Invalid database name.<br/>{{Error|BXDB0015|XQuery Errors#BaseX Errors}} No backup found.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:restore("DB")}} restores the database {{Code|DB}}.<br />
* {{Code|db:restore("DB-2014-03-13-18-05-45")}} restores the database {{Code|DB}} from the backup file with the given timestamp.<br />
|}<br />
<br />
==db:optimize==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:optimize|$db as xs:string|empty-sequence()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean|empty-sequence()}}<br/>{{Func|db:optimize|$db as xs:string, $all as xs:boolean, $options as map(xs:string, xs:string)|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Optimizes the meta data and indexes of the database {{Code|$db}}.<br/>If {{Code|$all}} is {{Code|true}}, the complete database will be rebuilt.<br/>The {{Code|$options}} argument can be used to control indexing. The syntax is identical to the [[#db:create|db:create]] function: Allowed options are all [[Options#Indexing|indexing]] and [[Options#Full-Text|full-text]] options. [[Options#UPDINDEX|UPDINDEX]] is only allowed if {{Code|$all}} is {{Code|true}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|FOUP0002|XQuery Errors#Update Errors}} an error occurred while optimizing the database.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:optimize("DB")}} optimizes the database structures of the database {{Code|DB}}.<br />
* <code>db:optimize("DB", true(), map { 'ftindex': true() })</code> optimizes all database structures of the database {{Code|DB}} and creates a full-text index.<br />
|}<br />
<br />
==db:rename==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:rename|$db as xs:string, $path as xs:string, $newpath as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Renames document(s), specified by {{Code|$path}} to {{Code|$newpath}} in the database {{Code|$db}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0008|#Errors}} new document names would be empty.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:rename("DB", "docs/dir/doc.xml", "docs/dir/newdoc.xml")}} renames the document {{Code|docs/dir/doc.xml}} to {{Code|docs/dir/newdoc.xml}} in the database {{Code|DB}}.<br />
* {{Code|db:rename("DB", "docs/dir", "docs/newdir")}} renames all documents with paths beginning with {{Code|docs/dir}} to paths beginning with {{Code|docs/newdir}} in the database {{Code|DB}}.<br />
|}<br />
<br />
==db:replace==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:replace|$db as xs:string, $path as xs:string, $input as item()|empty-sequence()}}<br/>{{Func|db:replace|$db as xs:string, $path as xs:string, $input as item(), $options as map(xs:string, xs:string)|empty-sequence()|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Replaces a document, specified by {{Code|$path}}, in the database {{Code|$db}} with the content of {{Code|$input}}, or adds it as a new document.<br/>The {{Code|$options}} argument can be used to control parsing. The syntax is identical to the [[#db:create|db:create]] function: Allowed options are all [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options in lower case.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0014|#Errors}} {{Code|$path}} points to a directory.<br/>{{Error|FODC0002|XQuery Errors#Functions Errors}} {{Code|$input}} is a string representing a path, which cannot be read.<br/>{{Error|FOUP0001|XQuery Errors#Update Errors}} {{Code|$input}} is neither a string nor a document node.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:replace("DB", "docs/dir/doc.xml", "/home/dir/doc.xml")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the content of the file {{Code|/home/dir/doc.xml}}.<br />
* {{Code|db:replace("DB", "docs/dir/doc.xml", "<a/>")}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with {{Code|&lt;a/&gt;}}.<br />
* {{Code|db:replace("DB", "docs/dir/doc.xml", document { <a/> })}} replaces the content of the document {{Code|docs/dir/doc.xml}} in the database {{Code|DB}} with the specified document node.<br />
The following query can be used to import files from a directory to a database:<br />
<pre class="brush:xquery"><br />
let $source := '/home/john/xml/source'<br />
for $file in file:list($source, true())<br />
let $path := $source || $file<br />
where not(file:is-dir($path))<br />
return db:replace('db', $file, doc($path))<br />
</pre><br />
|}<br />
<br />
==db:store==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:store|$db as xs:string, $path as xs:string, $input as item()|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Stores a binary resource specified by {{Code|$input}} in the database {{Code|$db}} and the location specified by {{Code|$path}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|BXDB0003|#Errors}} the database is not ''persistent'' (stored on disk).<br/>{{Error|FODC0007|XQuery Errors#Functions Errors}} the specified path is invalid.<br/>{{Error|FOUP0002|XQuery Errors#Update Errors}} the resource cannot be stored at the specified location.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:store("DB", "video/sample.mov", file:read-binary('video.mov'))}} stores the addressed video file at the specified location.<br />
|}<br />
<br />
==db:output==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:output|$result as item()*|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|This function can be used to both perform updates and return results in a single query. The argument of the function will be evaluated, and the resulting items will be cached and returned after the updates on the ''pending update list'' have been processed. As nodes may be updated, they will be copied before being cached.<br/>The function can only be used together with [[XQuery Update#Updating Expressions|updating expressions]]; if the function is called within a transform expression, its results will be discarded.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:output("Prices have been deleted."), delete node //price}} deletes all {{Code|price}} elements in a database and returns an info message.<br />
|}<br />
<br />
==db:output-cache==<br />
<br />
{{Mark|Introduced with Version 8.2}}:<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:output-cache||item()*}}<br />
|-<br />
| '''Summary'''<br />
|Returns the items that have been cached by [[#db:output|db:output]]. It can be used to check which items will eventually be returned as result of an updating function.<br/>This function is non-deterministic: Its will return different results before and after items have been cached. It is e. g. useful when writing [[Unit Module|unit tests]].<br />
|}<br />
<br />
==db:flush==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:flush|$db as xs:string|empty-sequence()}}<br />
|-<br />
| '''Summary'''<br />
|Explicitly flushes the buffers of the database {{Code|$db}}. This command is only useful if [[Options#AUTOFLUSH|AUTOFLUSH]] has been set to {{Code|false}}.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|}<br />
<br />
=Helper Functions=<br />
<br />
==db:name==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:name|$node as node()|xs:string}}<br />
|-<br />
| '''Summary'''<br />
|Returns the name of the database in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0001|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.<br />
|}<br />
<br />
==db:path==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:path|$node as node()|xs:string}}<br />
|-<br />
| '''Summary'''<br />
|Returns the path of the database document in which the specified [[#Database Nodes|database node]] {{Code|$node}} is stored.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0001|#Errors}} {{Code|$nodes}} contains a node which is not stored in a database.<br />
|}<br />
<br />
==db:exists==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:exists|$db as xs:string|xs:boolean}}<br/>{{Func|db:exists|$db as xs:string, $path as xs:string|xs:boolean}}<br />
|-<br />
| '''Summary'''<br />
|Checks if the database {{Code|$db}} or the resource specified by {{Code|$path}} exists. {{Code|false}} is returned if a database directory has been addressed.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:exists("DB")}} returns {{Code|true}} if the database {{Code|DB}} exists.<br />
* {{Code|db:exists("DB", "resource")}} returns {{Code|true}} if {{Code|resource}} is an XML document or a raw file.<br />
|}<br />
<br />
==db:is-raw==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:is-raw|$db as xs:string, $path as xs:string|xs:boolean}}<br />
|-<br />
| '''Summary'''<br />
|Checks if the specified resource in the database {{Code|$db}} and the path {{Code|$path}} exists, and if it is a [[Binary Data|binary resource]].<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:is-raw("DB", "music/01.mp3")}} returns {{Code|true}}.<br />
|}<br />
<br />
==db:is-xml==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:is-xml|$db as xs:string, $path as xs:string|xs:boolean}}<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br />
|-<br />
| '''Summary'''<br />
|Checks if the specified resource in the database {{Code|$db}} and the path {{Code|$path}} exists, and if it is an XML document.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:is-xml("DB", "dir/doc.xml")}} returns {{Code|true}}.<br />
|}<br />
<br />
==db:content-type==<br />
<br />
{| width='100%'<br />
|-<br />
| width='120' | '''Signatures'''<br />
|{{Func|db:content-type|$db as xs:string, $path as xs:string|xs:string}}<br />
|-<br />
| '''Summary'''<br />
|Retrieves the content type of a resource in the database {{Code|$db}} and the path {{Code|$path}}.<br/>The file extension is used to recognize the content-type of a resource stored in the database. Content-type {{Code|application/xml}} will be returned for any XML document stored in the database, regardless of its file name extension.<br />
|-<br />
| '''Errors'''<br />
|{{Error|BXDB0002|XQuery Errors#BaseX Errors}} The addressed database does not exist or could not be opened.<br/>{{Error|FODC0002|XQuery Errors#Functions Errors}} the addressed resource is not found or cannot be retrieved.<br />
|-<br />
| '''Examples'''<br />
|<br />
* {{Code|db:content-type("DB", "docs/doc01.pdf")}} returns {{Code|application/pdf}}.<br />
* {{Code|db:content-type("DB", "docs/doc01.xml")}} returns {{Code|application/xml}}.<br />
* {{Code|db:content-type("DB", "docs/doc01")}} returns {{Code|application/xml}}, if {{Code|db:is-xml("DB", "docs/doc01")}} returns {{Code|true}}.<br />
|}<br />
<br />
=Errors=<br />
<br />
{| class="wikitable" width="100%"<br />
! width="110"|Code<br />
|Description<br />
|-<br />
|{{Code|BXDB0001}}<br />
|The referenced XML node is no [[#Database Nodes|database node]], i.e. it is neither stored in a database nor represented as database fragment.<br />
|-<br />
|{{Code|BXDB0002}}<br />
|The addressed database does not exist or could not be opened.<br />
|-<br />
|{{Code|BXDB0003}}<br />
|The addressed database is not ''persistent'' (stored on disk).<br />
|-<br />
|{{Code|BXDB0004}}<br />
|The database lacks an index structure required by the called function.<br />
|-<br />
|{{Code|BXDB0005}}<br />
|A query is expected to exclusively return [[#Database Nodes|database nodes]] of a single database.<br />
|-<br />
|{{Code|BXDB0006}}<br />
|A database path addressed with {{Code|doc()}} contains more than one document.<br />
|-<br />
|{{Code|BXDB0007}}<br />
|A database cannot be updated because it is opened by another process.<br />
|-<br />
|{{Code|BXDB0008}}<br />
|Database paths cannot be renamed to empty strings.<br />
|-<br />
|{{Code|BXDB0009}}<br />
|The addressed database id or pre value is out of range.<br />
|-<br />
|{{Code|BXDB0011}}<br />
|The name of the specified database is invalid.<br />
|-<br />
|{{Code|BXDB0012}}<br />
|A database can only be created once.<br />
|-<br />
|{{Code|BXDB0013}}<br />
|The number of specified inputs and paths differs.<br />
|-<br />
|{{Code|BXDB0014}}<br />
|Path points to a directory.<br />
|}<br />
<br />
=Changelog=<br />
<br />
;Version 8.3<br />
* Updated: [[#db:list-details|db:list-details]] also returns size (#nodes) of document resources<br />
<br />
;Version 8.2<br />
* Added: [[#db:output-cache|db:output-cache]]<br />
* Removed: db:event<br />
<br />
;Version 7.9<br />
* Updated: parsing options added to [[#db:create|db:create]], [[#db:add|db:add]] and [[#db:replace|db:replace]].<br />
* Updated: allow [[Options#UPDINDEX|UPDINDEX]] if {{Code|$all}} is {{Code|true}}.<br />
<br />
;Version 7.8.2<br />
* Added: [[#db:alter|db:alter]], [[#db:copy|db:copy]], [[#db:create-backup|db:create-backup]], [[#db:drop-backup|db:drop-backup]], [[#db:restore|db:restore]]<br />
<br />
;Version 7.8<br />
* Removed: db:fulltext (use [[Full-Text Module#ft:search|ft:search]] instead)<br />
<br />
;Version 7.7<br />
<br />
* Added: [[#db:export|db:export]], [[#db:name|db:name]], [[#db:path|db:path]]<br />
* Updated: {{Code|$options}} argument added to [[#db:create|db:create]] and [[#db:optimize|db:optimize]].<br />
* Updated: the functions no longer accept [[#Database Nodes|Database Nodes]] as reference. Instead, the name of a database must now be specified.<br />
<br />
;Version 7.6<br />
<br />
* Updated: [[#db:create|db:create]]: allow more than one input and path.<br />
<br />
;Version 7.5<br />
<br />
* Updated: [[#db:add|db:add]]: input nodes will be automatically converted to document nodes<br />
* Added: [[#db:backups|db:backups]]<br />
* Added: [[#db:create|db:create]]<br />
* Added: [[#db:drop|db:drop]]<br />
<br />
;Version 7.3<br />
<br />
* Added: [[#db:flush|db:flush]]<br />
<br />
;Version 7.2.1<br />
<br />
* Added: [[#db:text-range|db:text-range]], [[#db:attribute-range|db:attribute-range]], [[#db:output|db:output]]<br />
<br />
;Version 7.1<br />
<br />
* Added: [[#db:list-details|db:list-details]], [[#db:content-type|db:content-type]]<br />
* Updated: [[#db:info|db:info]], [[#db:system|db:system]], [[#db:retrieve|db:retrieve]]<br />
<br />
;Version 7.0<br />
<br />
* Added: [[#db:retrieve|db:retrieve]], [[#db:store|db:store]], [[#db:exists|db:exists]], [[#db:is-raw|db:is-raw]], [[#db:is-xml|db:is-xml]]<br />
* Updated: [[#db:list|db:list]], [[#db:open|db:open]], [[#db:add|db:add]]<br />
<br />
[[Category:XQuery]]</div>Marco Lettere