eXistDB的简单XQuery函数示例

1、计算阶乘的函数

xquery version "3.0";

declare function local:fact($n as xs:integer) {
    if ($n eq 1) then
        $n
    else
        $n * local:fact($n - 1)
    ,
    util:log('warn','n is ' || $n)
};

local:fact(5)

2、记录日志的函数

xquery version "3.0";

declare function local:log-event($type as xs:string, $event as xs:string, $object-type as xs:string, $uri as xs:string)
{
    let $log-collection := "/db/Triggers"
    let $log := "log.xml"
    let $log-uri := concat($log-collection, "/", $log)
    return
    (
        (: create the log file if it does not exist :)
        if (not(doc-available($log-uri))) then
            xmldb:store($log-collection, $log, <triggers/>)
        else ()
        ,
        (: log the trigger details to the log file :)
        update insert <trigger event="{string-join(($type, $event, $object-type), '-')}" uri="{$uri}" timestamp="{current-dateTime()}"/> into doc($log-uri)/triggers
    )
};

local:log-event("before", "create", "collection", "$uri")

Leave a Reply

Your email address will not be published. Required fields are marked *

*