XRX/Autoincrement File ID
From Wikibooks, the open-content textbooks collection
< XRX
Contents |
[edit] Motivation
You want to create a new file and automatically create a new identifier for the file.
[edit] Method
Create an XML file with a single counter in it. Use this counter as part of the file name. After you confirm that the file has been saved, increment the counter using the update function.
[edit] Sample XML File to Hold Next ID
<data> <next-id>47</next-id> </data>
[edit] Sample Code for save-new.xq
xquery version "1.0";
declare namespace exist = "http://exist.sourceforge.net/NS/exist";
declare namespace xmldb = "http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare option exist:serialize "method=html media-type=text/html indent=yes";
(: save a new task - filename: save-new.xq - change base path if this changes. :)
(: this only works with POST data :)
let $my-doc := request:get-data()
(: the base directory URL for this XQuery. Use this to put links in the result page. :)
let $base-path := substring-before(request:get-url(), '/save-new.xq')
(: these paths must start with '/db' and are used for passing to doc() :)
let $data-collection := '/db/app/data'
let $next-id-file-path := '/db/app/edit/next-id.xml'
let $next-id := doc($next-id-file-path)/data/next-id/text()
(: use this as an arugment for for store command :)
let $new-term-base-file-name := concat($next-id, '.xml')
(: use this for doc :)
let $new-term-file-path := concat($data-collection, '/', $new-term-base-file-name)
let $login := xmldb:collection($data-collection, 'mylogin', 'mypassword')
(: store the new term in the given collction :)
let $store-return-status := xmldb:store($data-collection, $new-term-base-file-name, $my-doc)
(: increment the next-id by one for the next document :)
let $retCode1 := update replace doc($next-id-file-path)/data/next-id/text()
with ($next-id + 1)
(: note that next-id is now the next item so the current is next-id -1 :)
(: update the ID in the new document. Note that the saved document must have
the ID in the path /app/id. :)
let $retCode2 := update replace doc($new-term-file-path)/app/id with <id>{$next-id - 1}</id>
return
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Save Confirmation</title>
</head>
<body>
<h1>New file has been saved. id={$next-id - 1}.</h1>
</body>
</html>