Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active August 29, 2015 14:21
Show Gist options
  • Save CliffordAnderson/80c68ea43557446b8331 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/80c68ea43557446b8331 to your computer and use it in GitHub Desktop.
Experiments with XQuery, RESTXQ, and MVC in BaseX
module namespace controller = 'http://www.library.vanderbilt.edu/baudelaire/controller';
import module namespace view = 'http://www.library.vanderbilt.edu/baudelaire/view' at 'view.xqm';
import module namespace model = 'http://www.library.vanderbilt.edu/baudelaire/model' at 'model.xqm';
declare
%rest:path("/")
%rest:GET
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function controller:contents() {
view:display-contents("Corpus Baudelaire", <p>Hello!</p>)
};
declare
%rest:path("/about")
%rest:GET
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function controller:about() {
let $title := "About Corpus Baudelaire"
let $content := <p>Here is some information about the corpus baudelaire project.</p>
return view:display-contents($title, $content)
};
declare
%rest:path("/credits")
%rest:GET
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function controller:credits() {
let $title := "Credits for Corpus Baudelaire"
let $content := model:credits()
return view:display-contents($title, $content)
};
declare
%rest:path("/contents")
%rest:GET
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function controller:toc() {
let $title := "Contents of Corpus Baudelaire"
let $content := model:toc()
return view:display-contents($title, $content)
};
declare
%rest:path("/poem/{$name}")
%rest:GET
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function controller:poem($name) {
let $title := "Corpus Baudelaire: The " || $name || " Poem"
let $content := <p>Here is some information about the {$name} poem.</p>
return view:display-contents($title, $content)
};
declare
%rest:path("/error404")
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function controller:error404() {
let $title := "Corpus Baudelaire"
let $content := <p>Error! This page does not exist!</p>
return view:display-contents($title, $content)
};
module namespace model = 'http://www.library.vanderbilt.edu/baudelaire/model';
declare namespace tei = "http://www.tei-c.org/ns/1.0";
declare function model:list-names() {
let $names := fn:collection("fleurs-du-mal")//tei:respStmt/tei:name/text()
for $name in fn:distinct-values($names)
order by fn:replace($name, ".* (.*$)", "$1")
return $name
};
declare function model:credits() as element(div) {
<div>
<h2>Credits</h2>
<p>Many hands have contributed to the Corpus Baudelaire Project. Key contributors include:
<ul>
{
for $name in model:list-names()
return <li>{$name}</li>
}
</ul>
</p>
</div>
};
declare function model:toc() as element(div) {
<div>
<h2>Table of Contents</h2>
<ul>
{
for $title in fn:collection("fleurs-du-mal")//tei:head/tei:title[@type="main"]/text()
return <li>{$title}</li>
}
</ul>
</div>
};
module namespace view = 'http://www.library.vanderbilt.edu/baudelaire/view';
declare function view:display-contents($title as xs:string?, $content as element()*) {
<html>
<head>
<title>{$title}</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
<!-- Optional theme -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"/>
<!-- Latest compiled and minified JavaScript -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Corpus Baudelaire</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contents">Contents</a></li>
<li><a href="/credits">Team</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>{$title}</h1>
<div>{$content}</div>
</div>
</div>
</body>
</html>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment