Jump to content

XRX/Move a Resource

From Wikibooks, open books for an open world
< XRX

Motivation

[edit | edit source]

You want to create an XForms front end to an XQuery script that will move a resource from one collection to another collection or you want to move a collection from one location to another location.

Method

[edit | edit source]

We will build a simple XForms front end to the xmldb:move() operator. The syntax of the move operators is the following:

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

Where:

  $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

If you are going to move a complete collection the format is:

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

On this case the operation:

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

would result in the bar collection being located inside the foo collection:

  /db/foo/bar

To test this application create two test collections in your eXist database such as:

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

Sample XForms Client

[edit | edit source]

Create the following xhtml file:

<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>

Sample XQuery on Server

[edit | edit source]

The following XQuery named "move.xq" should be placed in the same collection as the move.xhtml file.

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>

Move a Collection

[edit | edit source]
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>

Discussion

[edit | edit source]

Now that you have the core move code done, you can enhance the program to browse to a specific collection for the source and browse to a specific collection for the destination. You can also create a listing of the resources in the source and destination folders.


Back: Autoincrement File ID Next: Save File Dialog