xdmp:document-load

xdmp:document-load(
   $location as xs:string,
   [$options as node()?]
) as empty-sequence()

Summary

Inserts a new document with the specified URI. If a document already exists at the URI, the function replaces the content in the existing document as an update operation.

Parameters
$location The location of the input document. If the scheme of the location is HTTP (that is, if the string starts with "http://"), then the document is requested over HTTP. If the scheme is file (that is, if the string starts with "file://"), then the document is requested over file protocol from the local filesystem. Otherwise, the document is fetched from the local filesystem. On the filesystem, the path can be fully qualifed or relative. Relative pathnames are resolved from the directory in which MarkLogic Server is installed.
$options The options node for this load operation. The default value is (). The node for the xdmp:document-load options must be in the xdmp:document-load namespace. This parameter can also include option elements in the xdmp:http namespace for the HTTP parameters.

The xdmp:document-load options include:

<uri>

The URI of the document to be loaded. If omitted, then the location is used for the URI.

<permissions>

Security permission elements corresponding to the permissions for the document. If not supplied, the current user's default permissions are applied. The default value used for $permissions can be obtained by calling xdmp:default-permissions(). A document that is created by a non-admin user (that is, by any user who does not have the admin role) must have at least one update permission, otherwise the creation will throw an XDMP-MUSTHAVEUPDATE exception.

<collections>

The collection URIs for collections to which this document belongs. If not supplied, the document is added to the current user's default collections (the collections returned from xdmp:default-collections()). For each collection that is protected, the user must have permissions to update that collection or have the any-collection privilege. For each unprotected collection, the user must have the unprotected-collections privilege. The <collections> element consists of one or more <collection> child elements. For example:
    <collections>
      <collection>myCollection1</collection>
      <collection>myCollection2</collection>
    </collections> 

<quality>

The quality of this document. A positive value increases the relevance score of the document in text search functions. The converse is true for a negative value. The default value is 0.

<default-namespace>

The namespace to use if there is no namespace at the root node of the document. The default value is "".

<repair>

A value of full specifies that malformed XML content be repaired. A value of none specifies that malformed XML content is rejected. If no repair option is explicitly specified, the default is implicitly specified by the XQuery version of the caller. In XQuery 1.0 and 1.0-ml the default is none. In XQuery 0.9-ml the default is full. This option has no effect on binary or text documents.

<format>

A value of text specifies to get the document as a text document, regardless of the URI specified. A value of binary specifies to get the document as a binary document, regardless of the URI specified. A value of xml specifies to get the document as an XML document, regardless of the URI specified.

<default-language>

The language to specify in an xml:lang attribute on the root element node if the root element node does not already have an xml:lang attribute. If default-language is not specified, then nothing is added to the root element node.

<forests>

Specifies the ID of the forest in which this document is inserted. Each forest ID is in a <forest> child element and is of type xs:unsignedLong. If the document already exists in the database, it will remain in its existing forest. If no such forest exists or if no such forest is attached to the context database, an error is raised. If multiple forests are specified, the document is inserted into one of the specifed forests. If the document already exists and the forest in which it is stored is set to delete-only, then you must specify the forest IDs to include one or more forests that allow updates, otherwise an exception is thrown.

If you have local disk failover enabled, specify the ID of the master forest. In the event of a failover, MarkLogic server will automatically redirect documents to the replica forest. Specify the ID of the replica forest will result in a "forest not in database" error.

<encoding>

Specifies the encoding to use when reading the document into MarkLogic Server. Supported values include UTF-8, ISO-8859-1, as well as many other popular encodings. See the Search Developer's Guide for a list of character set encodings by language. All encodings will be translated into UTF-8 from the specified encoding. The string specifed for the encoding option will be matched to an encoding name according to the Unicode Charset Alias Matching rules (http://www.unicode.org/reports/tr22/#Charset_Alias_Matching). An automatic encoding detector will be used if the value auto is specified. If no encoding can be detected, the encoding defaults to UTF-8. If no encoding option is specified and you are using a path with an http:// scheme, the encoding defaults to the encoding specified in the http header (if an encoding header is specified), otherwise it defaults to UTF-8.

Required Privileges

http://marklogic.com/xdmp/privileges/xdmp-document-load

If a new document is inserted, you also need the unprotected-uri privilege (only if the URI is not protected), the any-uri privilege, or an appropriate URI privilege.

If adding an unprotected collection to a document, the unprotected-collections privilege (http://marklogic.com/xdmp/privileges/unprotected-collections) is needed; if adding a protected collection, the user must have either permissions to update the collection or the any-collection privilege (http://marklogic.com/xdmp/privileges/any-collection).

Usage Notes

When selecting documents over HTTP (where the $location parameter begins with http://), the response from the webserver is loaded into the database, regardless of what the headers returned from the webserver indicate. For example, if the webserver returns a 404 (file not found), then the response page that says "file not found" is loaded into the database. If you want to examine the headers before loading the document, use xdmp:http-get (combined with xdmp:document-insert) instead, as xdmp:http-get allows you to examine the headers returned from the HTTP server.

Example

xdmp:document-load("c:\myFile.xml",
    <options xmlns="xdmp:document-load">
      <uri>/documents/myFile.xml</uri>
      <repair>none</repair>
      <permissions>{xdmp:default-permissions()}</permissions>
    </options>)

=> Loads the document with a URI "/documents/myFile.xml"
   and does not perform tag repair during the load.


Example

xdmp:document-load("http://myCompany.com/file.xml",
    <options xmlns="xdmp:document-load"
             xmlns:http="xdmp:http">
      <uri>/documents/myFile.xml</uri>
      <repair>none</repair>
      <permissions>{xdmp:default-permissions()}</permissions>
      <format>xml</format>
      <http:authentication>
          <http:username>user</http:username>
          <http:password>pass</http:password>
      </http:authentication>
    </options>)

=> Loads the document with a URI "/documents/myFile.xml"
   from the server http://myCompany.com, sending the
   credentials user/pass. Tag repair is not performed
   during the load, and the document is loaded as xml.


Example

xdmp:document-load("c:\myFile.xml",
    <options xmlns="xdmp:document-load">
       <uri>/documents/myFile.xml</uri>
       <permissions>{xdmp:default-permissions()}</permissions>
       <collections>
          <collection>myCollection1</collection>
          <collection>myCollection2</collection>
       </collections>
       <repair>full</repair>
       <forests>
         <forest>{xdmp:forest("myForest")}</forest>
       </forests>
    </options> )

=> Loads the document with a URI "/documents/myFile.xml"
   performing tag repair during the load, adding the
   document to the "myCollection1" and "myCollection2"
   collections, and loading the document into the forest
   named "myForest".


Comments

  • Why oh why is the relative file location relative to the MarkLogic installation folder? The current xquery script file would be so much more convenient ...
  • Tip: If the first argument is "rest::" then the document will be streamed directly from the PUT or POST request body.  See Modules/MarkLogic/rest-api/models/document-model-update.xqy.