Skip to content

Commit f0bef72

Browse files
committed
Refactor and cleanup XForms submission XQuery code
1 parent b352531 commit f0bef72

2 files changed

Lines changed: 521 additions & 399 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
xquery version "3.1";
2+
3+
module namespace rh = "http://localhost/manuForma/request-helper";
4+
5+
import module namespace request = "http://exist-db.org/xquery/request";
6+
7+
(:~
8+
: Get a HTTP request parameter.
9+
: Unlike request:get-parameter this will ignore parameters that have an empty string value, and return the default.
10+
:
11+
: @param $name the name of the HTTP request parameter
12+
: @param $default the default value to return if there is no valid parameter
13+
:
14+
: @return the value of the parameter if it is valid, or the default.
15+
:)
16+
declare function rh:request-param($name as xs:string, $default) {
17+
let $values := request:get-parameter($name, ())[. ne ""]
18+
return
19+
if (exists($values)) then
20+
$values
21+
else
22+
$default
23+
};
24+
25+
(:~
26+
: Get a HTTP request parameter as a boolean value.
27+
: Unlike request:get-parameter this will ignore parameters that have an empty string value.
28+
:
29+
: @param $name the name of the HTTP request parameter
30+
:
31+
: @return the boolean value of the parameter.
32+
:)
33+
declare function rh:request-param-bool($name as xs:string) as xs:boolean {
34+
lower-case(rh:request-param($name, "false")[1]) eq "true"
35+
};
36+
37+
(:~
38+
: Get HTTP request parameters.
39+
: Unlike request:get-parameter this will ignore parameters that have an empty string value, and return the default.
40+
:
41+
: @param $name-prefix the name prefix of the HTTP request parameters
42+
: @param $default the default value to return if there is no valid parameter
43+
:
44+
: @return a map or the names and values of the parameters if valid, or the default.
45+
:)
46+
declare function rh:request-params($name-prefix as xs:string, $default-value) as map(xs:string, xs:string*)* {
47+
for $name in request:get-parameter-names()[starts-with(., $name-prefix)]
48+
let $value := rh:request-param($name, $default-value)
49+
return
50+
if (exists($value)) then
51+
map { "name": $name, "value": $value }
52+
else
53+
()
54+
};

0 commit comments

Comments
 (0)