XQuery/加法器
外观
< XQuery
我们想创建一个简单的 XQuery,它接受两个参数并返回这两个数字的总和。
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
(: get the parameters from the URL :)
let $arg1 := xs:integer(request:get-parameter("arg1", "1"))
let $arg2 := xs:integer(request:get-parameter("arg2", "5"))
return
<results>
<sum>{$arg1+$arg2}</sum>
</results>
像这样调用:http://kitwallace.co.uk/xqbook/interaction/adder.xq?arg1=123&arg2=456
。
<results>
<sum>579</sum>
</results>
为了将其扩展为交互式应用程序,我们可以扩展脚本以创建包含表单的 XHTML 文档。
该脚本从 URL 参数(如果有)计算新的总和,并返回一个包含表单的最小 XHTML 文档,该表单既报告总和,又提示输入新的值。请注意嵌入的 XQuery 表达式(在花括号中),它将计算出的值插值到创建的 XML 元素中。计算状态(累加器的值)保存在表单中的隐藏输入中。
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $sum := xs:integer(request:get-parameter("sum",0))
let $number := xs:integer(request:get-parameter("number","0"))
let $newSum := $sum + $number
return
<html>
<head><title>Accumulating Adder</title></head>
<body>
<h1>Accumulating Adder</h1>
<form>
{$newSum} + <input type="text" name="number" value="{$number}" />
<input type="hidden" name="sum" value="{$newSum}"/>
</form>
</body>
</html>
尝试这个http://kitwallace.co.uk/xqbook/interaction/adder2.xq
为了支持清除累加器的操作,我们可以向表单中添加几个提交按钮。'清除'操作的存在用于将输入设置为零。
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $action := request:get-parameter("action","")
let $sum :=
if ($action= "clear")
then 0
else xs:integer(request:get-parameter("sum",0))
let $number :=
if ($action = "clear")
then 0
else xs:integer(request:get-parameter("number",0))
let $newSum := $sum + $number
return
<html>
<head><title>Accumulating Adder</title></head>
<body>
<h1>Accumulating Adder</h1>
<form>
{$newSum} + <input type="text" name="number" value="{$number}" />
<input type="hidden" name="sum" value="{$newSum}"/>
<input type="submit" name="action" value="add"/>
<input type="submit" name="action" value="clear"/>
</form>
</body>
</html>
使用这个尝试:http://kitwallace.co.uk/xqbook/interaction/adder3.xq
保存此计算状态的另一种方法是使用会话变量。eXist 中的会话模块提供了必要的函数。
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $sum := if (exists(session:get-attribute("sum")))
then session:get-attribute("sum")
else 0
let $action := request:get-parameter("action","")
let $sum :=
if ( $action= "clear")
then 0
else $sum
let $number :=
if ($action = "clear")
then 0
else xs:integer(request:get-parameter("number","0"))
let $newSum := $sum + $number
let $s := session:set-attribute("sum",$newSum)
return
<html>
<head><title>Accumulating Adder</title></head>
<body>
<h1>Accumulating Adder</h1>
<form>
{$newSum} + <input type="text" name="number" value="{$number}" />
<input type="submit" name="action" value="add"/>
<input type="submit" name="action" value="clear"/>
</form>
</body>
</html>
使用这个尝试:http://kitwallace.co.uk/xqbook/interaction/adder4.xq
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare option exist:serialize "method=xml media-type=text/xml indent=yes omit-xml-declaration=no";
(: get the parameters from the URL :)
let $posted-data := request:get-data()
let $arg1 := $posted-data//arg1/text()
let $arg2 := $posted-data//arg2/text()
return
<results>
<sum>{$arg1+$arg2}</sum>
</results>