跳轉到內容

XRX/自動遞增檔案 ID

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

您希望建立一個新檔案並自動為該檔案建立一個新的識別符號。

建立一個包含單個計數器的 XML 檔案。使用此計數器作為檔名的一部分。確認檔案已儲存後,使用更新函式遞增計數器。

儲存下一個 ID 的示例 XML 檔案

[編輯 | 編輯原始碼]
<data>
   <next-id>47</next-id>
</data>

save-new.xq 的示例程式碼

[編輯 | 編輯原始碼]

以下示例適用於傳入的 HTTP POST 請求。

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)

(: add this line for testing in your local system or if you don't have group or RBAC set up :)
let $login := xmldb:collection($data-collection, 'mylogin', 'mypassword')

(: store the new document 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.  You can also use "update value" :)
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>

考慮併發訪問

[編輯 | 編輯原始碼]

與上面相同,但鎖定包含識別符號的節點。.

以下示例適用於傳入的 HTTP POST 請求。

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'

(: Get next-id current value and increment the node value with an exclusive lock of the node :)
let $next-id := util:exclusive-lock(
    doc($next-id-file-path)/data/next-id,
    let $current-id := doc($next-id-file-path)/data/next-id/text()
    let $retCode := update replace doc($next-id-file-path)/data/next-id/text() with ($current-id + 1)
    return ($current-id - 1))                      
(: Warning - Pitfall : $current-id evaluation changes after the update :)

(: 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)

(: add this line for testing in your local system or if you don't have group or RBAC set up :)
let $login := xmldb:collection($data-collection, 'mylogin', 'mypassword')

(: store the new document in the given collction :)
let $store-return-status := xmldb:store($data-collection, $new-term-base-file-name, $my-doc)

(: 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}</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}.</h1>
   </body>
</html>

新增和遞增屬性

[編輯 | 編輯原始碼]

請注意,插入屬性的語法如下

  update insert attribute {$attrName} {$attrValue} into expression

必須使用關鍵字 attribute,後面跟著屬性名稱和值。

如果您想將新 ID 儲存在屬性中,則語法為

  update insert attribute {'id'} {$next-id} into $doc/root

注意:如果 attribute 已經在 目標節點 中存在,則 insert 的作用與 replace 相同。

如果您要更新 ID,可以使用帶有 @ 的路徑表示式中的 replace 函式。

  update replace doc/root/@id with ($next-id + 1)

上一步:正則表示式構建器 下一步:移動資源

華夏公益教科書