跳轉到內容

XRX/移動資源

來自華夏公益教科書,自由的教科書
< XRX

您想建立一個 XForms 前端,用於執行將資源從一個集合移動到另一個集合,或將集合從一個位置移動到另一個位置的 XQuery 指令碼。

我們將構建一個簡單的 XForms 前端,用於 xmldb:move() 運算子。移動運算子的語法如下

xmldb:move($from-collection, $to-collection, $resource)

其中

  $from-collection is the path name to the collection you are moving the file from
  $to-collection is the path name to the collection you are moving the file to
  $resource in the name of the resource

如果要移動整個集合,格式為

xmldb:move($old-collection, $new-collection)

在這種情況下,操作

  xmldb:move('/db/bar', '/db/foo')

將導致 bar 集合位於 foo 集合內部

  /db/foo/bar

為了測試此應用程式,請在您的 eXist 資料庫中建立兩個測試集合,例如

  /db/from-collection
  /db/to-collection

示例 XForms 客戶端

[編輯 | 編輯原始碼]

建立以下 xhtml 檔案

<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:ev="http://www.w3.org/2001/xml-events">
    <head>
        <title>Move File</title>
        <style language="text/css">
       <![CDATA[
       @namespace xf url("http://www.w3.org/2002/xforms");
         .from .xf-value  {width: 40ex;}
         .to .xf-value  {width: 40ex;}
       ]]>
       </style>
       <xf:model>
           <xf:instance xmlns="">     
               <data>
                   <from>/db/from-collection</from>
                   <to>/db/to-collection</to>
                   <resource>foo.xml</resource>
               </data>
           </xf:instance>
            <xf:submission id="save" method="get" 
               action="move.xq"
               separator="&amp;"
               replace="all"/>
        </xf:model>
    </head>
    <body>
        <h3>Move Resource</h3>
         <xf:input ref="from" class="from">
            <xf:label>From Collection:</xf:label>
        </xf:input>
        <xf:input ref="to" class="to">
            <xf:label>To Collection:</xf:label>
        </xf:input>
       <xf:input ref="resource" class="resource">
            <xf:label>Resource:</xf:label>
        </xf:input>
        <xf:submit submission="save">
            <xf:label>Move File</xf:label>
        </xf:submit>
    </body>
</html>

伺服器上的示例 XQuery

[編輯 | 編輯原始碼]

以下名為“move.xq”的 XQuery 應該與 move.xhtml 檔案放在同一個集合中。

xquery version "1.0";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";

(: this logs you in and makes sure you have write access to the desitination folder :)
let $login := xmldb:collection('/db/to-collection', 'username', 'password')

let $from := request:get-parameter('from', '')
let $to := request:get-parameter('to', '')
let $resource := request:get-parameter('resource', '')

let $code := xmldb:move($from, $to, $resource)

return
<html>
   <head>
      <title>Move Result</title>
   </head>
   <body>
   <b>from:</b>{$from}<br/>
   <b>to:</b>{$to}<br/>
     <b>resource:</b>{$resource}<br/>
     <b>result code:</b>Result code is
     </body>
</html>

移動集合

[編輯 | 編輯原始碼]
xquery version "1.0";
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

(: this logs you in and makes sure you have write access to the destination folder :)
let $login := xmldb:login('/db', 'user-id', 'your-password')
 
let $from := request:get-parameter('from', '')
let $to := request:get-parameter('to', '')

(: You can put in checks to make sure that both from and to are not null here and that they both exist. :)
 
let $move-result-code := xmldb:move($from, $to)
 
return
<html>
   <head>
      <title>Move Collection Result</title>
   </head>
   <body>
      <h1>Move Collection Result</h1>
      <b>from:</b>{$from}<br/>
      <b>to:</b>{$to}<br/>
     <b>result code:</b>Result code is:{$move-result-code}
     </body>
</html>

現在您已經完成了核心移動程式碼,可以增強程式,以瀏覽到特定集合作為源,並瀏覽到特定集合作為目標。您還可以建立源和目標資料夾中資源的列表。


上一步: 自動遞增檔案 ID 下一步: 儲存檔案對話方塊

華夏公益教科書