更新简单文档

我们现在将向“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>