Database Reference
In-Depth Information
This functionality calls for a design that uses an XQuery module. The module should
contain an XQuery function that performs the logging. To make it easy to use, this
function must return zilch (a.k.a. the empty sequence in XPath lingo), so we can sim‐
ply call it from anywhere without influencing our output. Example 3-11 is a module
that does exactly this. Create this module and save it as /db/apps/exists101/log-
module.xqm .
Example 3-11. The logging module log-module.xqm
xquery version "3.0" encoding "UTF-8";
module namespace x101log = "http://www.exist-db.org/book/namespaces/exist101";
declare function x101log:add-log-message($message as xs:string)
as empty-sequence()
{
let $logfile-collection := "/db/apps/exist101/log"
let $logfile-name := "exist101-log.xml"
let $logfile-full := concat($logfile-collection, '/', $logfile-name)
let $logfile-created :=
if(doc-available($logfile-full))then
$logfile-full
else
xmldb:store($logfile-collection, $logfile-name, <eXist101-Log/>)
return
update insert
<LogEntry timestamp="{current-dateTime()}">{$message}</LogEntry>
into doc($logfile-full)/*
};
The module namespace definition at the top defines the code as an XQuery
module. A module must have a namespace, and it's customary to use some‐
thing that starts with a URL you own (to avoid name clashes). What comes
after that is up to you. Don't let the starting http:// fool you: it's just a string
without any further special meaning.
We declare a function that returns empty-sequence()? . In other words: noth‐
ing.
The first three let statements in the function simply define the logfile's loca‐
tion, name, and full name.
let $logfile-created := ... checks whether or not the logfile exists. If not,
it uses the xmldb:store extension function to create a new logfile with an
empty <eXist101-Log/> root element. If the logfile already exists, it simply
emulates the return value of xmldb:store by returning the logfile's name.
Search WWH ::




Custom Search