marklogic Very simple CRUD examples for XML documents Create a simple document


Example

This very simple snippet of XQuery can be executed in QueryConsole using the built-in "Documents" database as a sandbox. Each piece of the snippet has a comment to explain what the following line of code means.

xquery version "1.0-ml";
(: Let's first insert a simple document to get started :)
(: You need a URI- the location where the document is found in the database :)
let $uri := "/stuff/mysimpledocument.xml"
(: Documents need content. This is a simple XML node :)
let $doc := 
  <my-document>
    <body>Very simple example</body>
  </my-document>
(: Permissions are a big topic. For now, we'll use the default permissions. :)
let $permissions := xdmp:default-permissions()
(: Document collections are optional. One or more can be specified :)
(: Adding a collection for further examples that will use it. :)
let $collections := "simple-example"
(: Now we're just going to insert this document in the database :)
let $insert := xdmp:document-insert($uri,$doc,$permissions,$collections)
return <message>Document saved to {$uri}</message>

When this is executed, the console returns

<message>Document saved to /stuff/mysimpledocument.xml</message>

Now that the document exists, we can test additional operations...