XRX/Move a Resource

From Wikibooks, open books for an open world
Jump to: navigation, search


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.


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)


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


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


Sample XForms Client[edit]

Create the following xhtml file:

        <title>Move File</title>
        <style language="text/css">
       @namespace xf url("http://www.w3.org/2002/xforms");
         .from .xf-value  {width: 40ex;}
         .to .xf-value  {width: 40ex;}
           <xf:instance xmlns="">     
            <xf:submission id="save" method="get" 
        <h3>Move Resource</h3>
         <xf:input ref="from" class="from">
            <xf:label>From Collection:</xf:label>
        <xf:input ref="to" class="to">
            <xf:label>To Collection:</xf:label>
       <xf:input ref="resource" class="resource">
        <xf:submit submission="save">
            <xf:label>Move File</xf:label>

Sample XQuery on Server[edit]

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)

      <title>Move Result</title>
     <b>result code:</b>Result code is

Move a Collection[edit]

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)
      <title>Move Collection Result</title>
      <h1>Move Collection Result</h1>
     <b>result code:</b>Result code is:{$move-result-code}


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 IDNext: Save File Dialog