-
Peter Stadler authoredPeter Stadler authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
app-shared.xqm 6.62 KiB
xquery version "3.1" encoding "UTF-8";
module namespace app-shared="http://xquery.weber-gesamtausgabe.de/modules/app-shared";
import module namespace functx="http://www.functx.com";
import module namespace str="http://xquery.weber-gesamtausgabe.de/modules/str" at "str.xqm";
import module namespace templates="http://exist-db.org/xquery/templates" at "/db/apps/shared-resources/content/templates.xql";
declare variable $app-shared:FUNCTION_LOOKUP_ERROR := QName("http://xquery.weber-gesamtausgabe.de/modules/app-shared", "FunctionLookupError");
(:~
: Set an attribute to the value given in the $model map
:
: @author Peter Stadler
:)
declare function app-shared:set-attr($node as node(), $model as map(*), $attr as xs:string, $key as xs:string) as element() {
element {name($node)} {
$node/@*[not(name(.) = $attr)],
attribute {$attr} {$model($key)},
templates:process($node/node(), $model)
}
};
(:~
: Simply print the string value of $model($key)
:
: @author Peter Stadler
:)
declare
%templates:wrap
function app-shared:print($node as node(), $model as map(*), $key as xs:string) as xs:string? {
if ($model($key) castable as xs:string) then str:normalize-space($model($key))
else app-shared:join($node, $model, $key, '0', '')
};
(:~
: Simply print a sequence from the $model map by joining items with $separator
:
: @param $separator the separator for the string-join()
: @author Peter Stadler
:)
declare
%templates:wrap
%templates:default("max", "0")
%templates:default("separator", ", ")
function app-shared:join($node as node(), $model as map(*), $key as xs:string, $max as xs:string, $separator as xs:string) as xs:string? {
let $items :=
if($max castable as xs:integer and number($max) le 0) then $model($key)
else if($max castable as xs:integer and number($max) < count($model($key))) then (subsequence($model($key), 1, $max), '…')
else if($max castable as xs:integer and number($max) > 0) then subsequence($model($key), 1, $max)
else $model($key)
return
if (every $i in $items satisfies $i castable as xs:string) then string-join($items ! str:normalize-space(.), $separator)
else ()
};
(:~
: A non-wrapping alternative to the standard templates:each()
: Gets rid of the superfluous first list item
:
: @author Peter Stadler
:)
declare
%templates:default("max", "0")
%templates:default("callback", "0")
%templates:default("callbackArity", "2")
function app-shared:each($node as node(), $model as map(*), $from as xs:string, $to as xs:string, $max as xs:string, $callback as xs:string, $callbackArity as xs:string) as node()* {
let $items :=
if($max castable as xs:integer and $max != '0') then subsequence($model($from), 1, $max)