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>
以下文件可以链接到表单以进行格式化。[那个文件是什么?]