xquery version "1.0";
import module namespace saxonb = "http://cefn.com/saxonb/" at "../lib/libout.xquery";
declare namespace c="http://cefn.com/";
(:declare default element namespace "http://www.w3.org/1999/xhtml";:)
declare function c:href($node){
concat(encode-for-uri($node/@id), '.html')
};
declare function c:wrapcontent($title as xs:string, $content){
<html>
<head><title>{$title}</title></head>
<body>
<h1>{$title}</h1>
{$content}
</body>
</html>
};
declare function c:writeitem($item, $content){
saxonb:write-html(c:href($item),c:wrapcontent($item/@title,$content))
};
declare function c:writelink($item){
<a href="{c:href($item)}">{string($item/@title)}</a>
};
declare function c:writelist($items){
<ul>
{for $item in $items order by $item/@title return <li><a href="{c:href($item)}">{string($item/@title)}</a></li>}
</ul>
};
let $root := .,
$authors := $root/data/author,
$books := $root/data/book,
$collections := $root/data//collection,
$formats := for $formatname in distinct-values($collections/@type) return <format title="{$formatname}" id="{$formatname}"/>
return (
saxonb:write-html('index.html',
c:wrapcontent('Librivox catalog improved',
<div>
<h1>Browse records...</h1>
<ul>
<li><a href="author_list.html">by Author Name</a></li>
<li><a href="book_list.html">by Book Title</a></li>
<li><a href="format_list.html">by Download Format</a></li>
</ul>
<p/>
<p>If there are other shortcomings, errors or different list pages which would be useful, especially for
screenreaders, please <a href="http://cefn.com/blog/contact.html">contact me</a>.</p>
<p>I'm working on importing the descriptions for each title, but they are a mess of various standards
of HTML and throwing errors.</p>
<p><a href="http://cefn.com" >Cefn Hoile</a>, Mad Inventor</p>
<p>Snapshot at {current-dateTime()}</p>
</div>
)),
saxonb:write-html('author_list.html', c:wrapcontent('Author Name List',c:writelist($authors))),
saxonb:write-html('book_list.html', c:wrapcontent('Book Title List',
<ul>
{for $book in $books order by $book/@title return <li> {c:writelink($book)} [<i>{let $author := $authors[@id=$book/@authorid] return <a href="{c:href($author)}">{string($author/@id)}</a>}</i>] </li>}
</ul>
)),
saxonb:write-html('format_list.html', c:wrapcontent('Download Format List', c:writelist($formats))),
for $author in $authors return c:writeitem($author,
<ul>
{for $book in $books[@authorid=$author/@id] return <li><a href="{c:href($book)}">{string($book/@title)}</a></li>}
</ul>
),
for $book in $books return c:writeitem($book, (
(: if($book/@description) then saxon:parse(string($book/@description)) else () ,:)
<ul>
{for $collection in $book//collection return <li><a href="{$collection/@href}">{string($collection/@type)}</a></li>}
</ul>
)
),
for $format in $formats return c:writeitem($format,
<table>
{
for $collection in $collections[@type=$format/@title]
let $book := $collection/parent::book
order by number($book/@downloads) descending
return <tr><td width="30%">Downloads: {string($book/@downloads)}</td><td><a href="{$collection/@href}">{string($book/@title)}</a></td></tr>
}
</table>
)
)