forked from seiichiusa/ml-start
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.xqy
76 lines (66 loc) · 3.26 KB
/
search.xqy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
xquery version "1.0-ml";
declare function local:sanitizeInput($chars as xs:string?) {
fn:replace($chars,"[\]\[<>{}\\();%\+]","")
};
(: build the html :)
xdmp:set-response-content-type("text/html"),
'<!DOCTYPE html>',
<html>
<head>
<title>Search Books</title>
</head>
<body>
<form name="add-book" action="" method="post">
<fieldset>
<legend>Search Book</legend>
<label for="title">Title</label> <input type="text" id="title" name="title"/>
<label for="author">Author</label> <input type="text" id="author" name="author"/>
<label for="year">Year</label> <input type="text" id="year" name="year"/>
<label for="price">Price</label> <input type="text" id="price" name="price"/>
<label for="category">Category</label>
<select name="category" id="category">
<option/>
{
for $c in ('CHILDREN','FICTION','NON-FICTION')
return
<option value="{$c}">{$c}</option>
}
</select>
<input type="submit" value="Search"/>
</fieldset>
</form>
<div>
{
let $queryTerm := "al"
return
<div>
{
if (xdmp:get-request-method() eq "POST") then (
let $title as xs:string? := local:sanitizeInput(xdmp:get-request-field("title"))
let $author as xs:string? := local:sanitizeInput(xdmp:get-request-field("author"))
let $year as xs:string? := local:sanitizeInput(xdmp:get-request-field("year"))
let $price as xs:string? := local:sanitizeInput(xdmp:get-request-field("price"))
let $category as xs:string? := local:sanitizeInput(xdmp:get-request-field("category"))
return
<div>
<h1>Search Results: {$title}</h1>
<ul>
{
for $book in doc()/book
let $modCategory := if ( sql:trim($category) = '') then ( '.*' ) else ("^" || $category || "$")
where $book/title[matches(., $title, "i")]
and $book/author[matches(., $author, "i")]
and $book/year[matches(., $year, "i")]
and $book/price[matches(., $price, "i")]
and $book[@category[matches(., $category, "i")]]
return <li>{data($book/title)}</li>
}
</ul>
</div>
) else ()
}
</div>
}
</div>
</body>
</html>