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
创建以下 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="&"
               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>
以下名为“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>
现在您已经完成了核心移动代码,可以增强程序,以浏览到特定集合作为源,并浏览到特定集合作为目标。您还可以创建源和目标文件夹中资源的列表。