XRX/詞典編輯器
外觀
< XRX
你想要一個簡單的應用程式,它為每個表單儲存一個 XML 檔案。你希望多個使用者可以編輯各自的記錄,而不會產生衝突。
我們將每個詞條放在一個單獨的 XML 檔案中,這些檔案將按順序編號 - 1.xml、2.xml,依此類推。每個檔案定義一個詞條、其縮寫和其定義。
- 建立一個新的 eXist 集合(如果你使用 WebDAV 工具,則稱為資料夾),名為“dictionary”。
- 在“dictionary”集合中建立三個集合,名為“data”、“edit”和“views”。
<Term>
<id>1</id>
<TermName>Hello</TermName>
<TermDefinition>An informal greeting.</TermDefinition>
</Term>
<Term>
<id>2</id>
<TermName>Howdy</TermName>
<TermDefinition>An informal greeting used by cowboys.</TermDefinition>
</Term>
你的 XForms 應用程式將資料載入到一個例項中
<xf:instance src="1.xml"/>
你將 ID 號作為 URI 中的引數“id”傳遞給 XQuery “edit.xq”。“edit.xq”使用 ID 引數作為變數“$id”來構建表單。
呼叫格式:edit.xq?id=1
xquery version "1.0";
let $id := request:get-paramter(id, '')
return
<html>
...
<xf:instance src="{$id}.xml"/>
<xf:submission method="post" action="save.xq"/>
...
</html>
<Term>
<id/>
<TermName/>
<TermDefinition/>
</Term>
xquery version "1.0";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
(: this is the collection where we store all the terms, one file per term :)
let $collection := '/db/dictionary/data'
(: this is where the form "POSTS" documents to this XQuery using the POST method of a submission :)
let $term := request:get-data()
(: this logs you into the collection :)
let $collection := xmldb:collection('/db/dictionary/data', 'mylogin', 'mypassword')
(: get the next ID :)
let $next-id := doc(concat($collection, 'edit/next-id.xml'))/next-id/text()
let $file := concat($next-id, 'xml')
(: this creates a new file using the next-id and saves the term into the file :)
let $store := store($collection, $file, $term)
(: now that the save has been done we can increment the next-id :)
let $update := update insert doc("/db/dictionary/edit/next-id.xml")/data/next-id/text() ($next-id + 1)
(: now put in the id in the file we just created :)
let $update := update insert doc( concat($collection, '/', $file) )/Term/id/text() $next-id
<results>
<message>{$term/TermName/text(), $term/id/text()} has been saved.</message>
</results>
<data>
<next-id>3</next-id>
</data>
隨著你儲存越來越多的詞條,你可能想要建立一個詞條列表。你可以建立一個 XQuery 來列出所有詞條。對於每個詞條,你可以包含一個連結來檢視和編輯每個詞條。
xquery version "1.0";
let $collection := '/db/dictionary/data'
return
<html>
<head>
<title>Listing of Dictionary Terms</title>
</head>
<body>
<h1>Dictionary Terms</h1>
<ol>{for $term in collection($collection)/Term
let $id := $term/id/text()
return
<li>{$term/TermName/text()} : {$term/Defintion/text()}
<a href="view-term.xq?id={$id}">View</a>
<a href="../edit/edit.xq?id={$id}">Edit</a>
</li>
}</ol>
</body>
</html>
xquery version "1.0";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
(: update.xq :)
let $collection := '/db/dictionary/data'
(: this is where the form "POSTS" documents to this XQuery using the POST method of a submission :)
let $term := request:get-data()
(: this logs you into the collection :)
let $collection := xmldb:collection('/db/dictionary/data', 'mylogin', 'mypassword')
(: get the id out of the posted document :)
let $id := $term/id/text()
let $file := concat($id, '.xml')
(: this saves the new file and overwrites the old one :)
let $store := store($collection, $file, $term)
<results>
<message>{$term/TermName/text(), $term/id/text()} has been updated.</message>
</results>
edit.xq 從 URI 中獲取引數(例如,在表單“edit.xq?id=2”中),並將新元素放入例項中,或者將現有元素放入例項中。
xquery version "1.0";
declare option exist:serialize "method=html media-type=text/html indent=yes";
let $new := request:get-parameter('new', '')
let $id := request:get-parameter('id', '')
return
(: check for required parameters :)
if (not($new or $id))
then
<error>
<message>Parameter "new" and "id" are both missing. One of these two arguments is required for this web service.</message>
</error>
else
let $server-port := substring-before(request:get-url(), '/exist/rest/db/')
let $collection := '/db/dictionary/data'
(: put in the appropriate file name :)
let $file := if ($new)
then ('new-instance.xml')
else ( concat( $server-port, '/exist/rest', $collection, '/', $id, '.xml'))
return
<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>Edit Term</title>
<xf:model>
<!-- this line loads either the new instance or the current data file into the form model -->
<xf:instance xmlns="" src="{$file}"/>
</xf:model>
</head>
<body>
<xf:output ref="id">
<xf:label>ID:</xf:label>
</xf:output >
<xf:input ref="TermName" class="TermName">
<xf:label>Term:</xf:label>
</xf:input>
<xf:textarea ref="TermDefinition" class="TermDefinition">
<xf:label>TermDefinition:</xf:label>
</xf:textarea >
</body>
</html>
以下檔案可以連結到表單以進行格式化。[那個檔案是什麼?]