XQuery/Wikibook list of code links

From Wikibooks, open books for an open world
Jump to: navigation, search

Motivation[edit]

This Wikibook contains links to code samples executed on a University server. We need to keep track of all the links so that we can ensure that they remain live, so that all links can be executed by a test bed and to support changes to the directory structure or filenames.

Approach[edit]

The script is similar to the index script at the beginning, to get the list of pages in the book. Then it fetches each page and extracts the anchor tags whose href links to the UWE eXist site. The WikiBook page is linked from the page title and the actual URL is listed.


declare namespace h ="http://www.w3.org/1999/xhtml";
declare option exist:serialize "method=xhtml media-type=text/html";
 
let $book:= request:get-parameter("book","XQuery")
let $base := "http://en.wikibooks.org"
let $indexPage :=doc(concat($base,"/wiki/Category:",$book,"?x"))
let $pages := $indexPage//h:div[@id="mw-pages"]//h:li
return
 
<html>
   <head>
       <title>Index of {$book} code samples</title>
    </head>
    <body>
        <h1>Index of {$book} code samples</h1>
        <ul>
{
for $letter in distinct-values($pages/upper-case(substring(substring-after(.,'/'),1,1)))[string-length(.) = 1]
for $page in $pages[starts-with(upper-case(substring-after(.,'/')),$letter)]
let $title := string($page)
let $url := concat($base,$page/h:a/@href)
let $refs := doc($url)//h:a[starts-with(@href,"http://www.cems.uwe.ac.uk/xmlwiki")]
order by $title
return
   if (exists($refs))
   then 
      <div>
        <li><a href="{$url}">{$title}</a>
          <ul>
            {for $ref in  $refs
              return 
                 <li> {string( $ref /@href)} </li>
            }
         </ul>
        </li>
      </div>
    else  ()
}
   </ul>
   </body>
</html>