更新簡單文件

我們現在將向“my-document”元素新增一些額外的 XML 節點並更新文件。該片段再次包含用於解釋正在發生的事情的註釋。

xquery version "1.0-ml";
(: We are preserving the same URI as we used originally :)
let $uri := "/stuff/mysimpledocument.xml"
(: Need to get the existing contents so we can append to those :)
let $orig-content := fn:doc($uri)/my-document
(: Documents need content. This is a simple XML node :)
let $new-content := 
  <notes>
    <note>Anything can be changed</note>
    <note>New content is added in this example, but we could replace it all too</note>
  </notes>
(: Now to build the new xml. There's lots of ways to do this :)
(: line 17 inserts the original contents into this new node construct :)
(: line 18 inserts the $new-content after the original contents :)
let $new-doc-content :=
  <my-document>
    {$orig-content/node()}
    {$new-content}
  </my-document>
(: Leave permissions untouched... :)
let $permissions := xdmp:document-get-permissions($uri)
(: Leave collections untouched... :)
let $collections := xdmp:document-get-collections($uri)
(: Now we're just going to insert this document in the database :)
let $insert := xdmp:document-insert($uri,$new-doc-content,$permissions,$collections)
return <message>Document {$uri} updated</message>

執行時,返回訊息

<message>Document /stuff/mysimpledocument.xml updated</message>

現在執行上面的不同讀取命令以檢視更新的內容。它應該如下所示:

<my-document>
    <body>Very simple example</body>
    <notes>
        <note>Anything can be changed</note>
        <note>New content is added in this example, but we could replace it all too</note>
    </notes>
</my-document>