Fix the fn:abs declaration in fn.xqy (Functions and Operators 3.0).

(: XPath and XQuery Functions and Operators 3.0
 : W3C Recommendation 08 April 2014
 :
 : Reference: https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/
 :)
xquery version "3.0";

module  namespace fn = "http://www.w3.org/2005/xpath-functions";
declare namespace xs = "http://www.w3.org/2001/XMLSchema";

declare function fn:QName($paramURI as xs:string?, $paramQName as xs:string) as xs:QName external; (: §10.1.2 :)
declare function fn:abs($arg as xs:numeric?) as xs:numeric? external; (: §4.4.1 :)
declare function fn:adjust-date-to-timezone($arg as xs:date?) as xs:date? external; (: §9.6.2 :)
declare function fn:adjust-date-to-timezone($arg as xs:date?, $timezone as xs:dayTimeDuration?) as xs:date? external; (: §9.6.2 :)
declare function fn:adjust-dateTime-to-timezone($arg as xs:dateTime?) as xs:dateTime? external; (: §9.6.1 :)
declare function fn:adjust-dateTime-to-timezone($arg as xs:dateTime?, $timezone as xs:dayTimeDuration?) as xs:dateTime? external; (: §9.6.1 :)
declare function fn:adjust-time-to-timezone($arg as xs:time?) as xs:time? external; (: §9.6.3 :)
declare function fn:adjust-time-to-timezone($arg as xs:time?, $timezone as xs:dayTimeDuration?) as xs:time? external; (: §9.6.3 :)
declare function fn:analyze-string($input as xs:string?, $pattern as xs:string) as element(fn:analyze-string-result) external; (: §5.6.5 :)
declare function fn:analyze-string($input as xs:string?, $pattern as xs:string, $flags as xs:string) as element(fn:analyze-string-result) external; (: §5.6.5 :)
declare function fn:available-environment-variables() as xs:string* external; (: §14.8.9 :)
declare function fn:avg($arg as xs:anyAtomicType*) as xs:anyAtomicType? external; (: §14.5.2 :)
declare function fn:base-uri() as xs:anyURI? external; (: §2.5 :)
declare function fn:base-uri($arg as node()?) as xs:anyURI? external; (: §2.5 :)
declare function fn:boolean($arg as item()*) as xs:boolean external; (: §7.3.1 :)
declare function fn:ceiling($arg as xs:numeric?) as xs:numeric? external; (: §4.4.2 :)
declare function fn:codepoint-equal($comparand1 as xs:string?, $comparand2 as xs:string?) as xs:boolean? external; (: §5.3.5 :)
declare function fn:codepoints-to-string($arg as xs:integer*) as xs:string external; (: §5.2.1 :)
declare function fn:collection() as node()* external; (: §14.8.3 :)
declare function fn:collection($arg as xs:string?) as node()* external; (: §14.8.3 :)
declare function fn:compare($comparand1 as xs:string?, $comparand2 as xs:string?) as xs:integer? external; (: §5.3.4 :)
declare function fn:compare($comparand1 as xs:string?, $comparand2 as xs:string?, $collation as xs:string) as xs:integer? external; (: §5.3.4 :)
declare %variadic("xs:anyAtomicType?") function fn:concat($arg1 as xs:anyAtomicType?, $arg2 as xs:anyAtomicType?) as xs:string external; (: §5.4.1 :)
declare function fn:contains($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean external; (: §5.5.1 :)
declare function fn:contains($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean external; (: §5.5.1 :)
declare function fn:count($arg as item()*) as xs:integer external; (: §14.5.1 :)
declare function fn:current-date() as xs:date external; (: §15.4 :)
declare function fn:current-dateTime() as xs:dateTime external; (: §15.3 :)
declare function fn:current-time() as xs:time external; (: §15.5 :)
declare function fn:data() as xs:anyAtomicType* external; (: §2.4 :)
declare function fn:data($arg as item()*) as xs:anyAtomicType* external; (: §2.4 :)
declare function fn:dateTime($arg1 as xs:date?, $arg2 as xs:time?) as xs:dateTime? external; (: §9.3.1 :)
declare function fn:day-from-date($arg as xs:date?) as xs:integer? external; (: §9.5.10 :)
declare function fn:day-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.3 :)
declare function fn:days-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.3 :)
declare function fn:deep-equal($parameter1 as item()*, $parameter2 as item()*) as xs:boolean external; (: §14.2.3 :)
declare function fn:deep-equal($parameter1 as item()*, $parameter2 as item()*, $collation as string) as xs:boolean external; (: §14.2.3 :)
declare function fn:default-collation() as xs:string external; (: §15.7 :)
declare function fn:distinct-values($arg as xs:anyAtomicType*) as xs:anyAtomicType* external; (: §14.2.1 :)
declare function fn:distinct-values($arg as xs:anyAtomicType*, $collation as xs:string) as xs:anyAtomicType* external; (: §14.2.1 :)
declare function fn:doc($uri as xs:string?) as document-node()? external; (: §14.8.1 :)
declare function fn:doc-available($uri as xs:string?) as xs:boolean external; (: §14.8.2 :)
declare function fn:document-uri() as xs:anyURI? external; (: §2.6 :)
declare function fn:document-uri($arg as node()?) as xs:anyURI? external; (: §2.6 :)
declare function fn:element-with-id($arg as xs:string*) as element()* external; (: §14.7.2 :)
declare function fn:element-with-id($arg as xs:string*, $node as node()) as element()* external; (: §14.7.2 :)
declare function fn:empty($arg as item()*) as xs:boolean external; (: §14.1.2 :)
declare function fn:encode-for-uri($uri-part as xs:string?) as xs:string external; (: §6.2 :)
declare function fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean external; (: §5.5.3 :)
declare function fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean external; (: §5.5.3 :)
declare function fn:environment-variable($name as xs:string) as xs:string? external; (: §14.8.8 :)
declare function fn:error() as none external; (: §3.1 :)
declare function fn:error($error as xs:QName) as none external; (: §3.1 :)
declare function fn:error($error as xs:QName?, $description as xs:string) as none external; (: §3.1 :)
declare function fn:error($error as xs:QName?, $description as xs:string, $error-object as item()*) as none external; (: §3.1 :)
declare function fn:escape-html-uri($uri as xs:string?) as xs:string external; (: §6.4 :)
declare function fn:exactly-one($arg as item()*) as item() external; (: §14.3.3 :)
declare function fn:exists($arg as item()*) as xs:boolean external; (: §14.1.3 :)
declare function fn:false() as xs:boolean external; (: §7.1.2 :)
declare function fn:filter($seq as item()*, $f as function(item()) as xs:boolean) as item()* external; (: §16.2.2 :)
declare function fn:floor($arg as xs:numeric?) as xs:numeric? external; (: §4.4.3 :)
declare function fn:fold-left($seq as item()*, $zero as item()*, $f as function(item()*, item()) as item()*) as item()* external; (: §16.2.3 :)
declare function fn:fold-right($seq as item()*, $zero as item()*, $f as function(item()*, item()) as item()*) as item()* external; (: §16.2.4 :)
declare function fn:for-each($seq as item()*, $f as function(item()) as item()*) as item()* external; (: §16.2.1 :)
declare function fn:for-each-pair($seq1 as item()*, $seq2 as item()*, $f as function(item(), item()) as item()*) as item()* external; (: §16.2.5 :)
declare function fn:format-date($value as xs:date?, $picture as xs:string) as xs:string? external; (: §9.8.2 :)
declare function fn:format-date($value as xs:date?, $picture as xs:string, $language as xs:string?, $calendar as xs:string?, $place as xs:string?) as xs:string? external; (: §9.8.2 :)
declare function fn:format-dateTime($value as xs:dateTime?, $picture as xs:string) as xs:string? external; (: §9.8.1 :)
declare function fn:format-dateTime($value as xs:dateTime?, $picture as xs:string, $language as xs:string?, $calendar as xs:string?, $place as xs:string?) as xs:string? external; (: §9.8.1 :)
declare function fn:format-integer($value as xs:integer?, $picture as xs:string) as xs:string external; (: §4.6.1 :)
declare function fn:format-integer($value as xs:integer?, $picture as xs:string, $lang as xs:string?) as xs:string external (: §4.6.1 :)
declare function fn:format-number($value as xs:numeric?, $picture as xs:string) as xs:string external; (: §4.7.2 :)
declare function fn:format-number($value as xs:numeric?, $picture as xs:string, $decimal-format-name as xs:string?) as xs:string external; (: §4.7.2 :)
declare function fn:format-time($value as xs:time?, $picture as xs:string) as xs:string? external; (: §9.8.3 :)
declare function fn:format-time($value as xs:time?, $picture as xs:string, $language as xs:string?, $calendar as xs:string?, $place as xs:string?) as xs:string? external; (: §9.8.3 :)
declare function fn:function-arity($func as function(*)) as xs:integer external; (: §16.1.3 :)
declare function fn:function-lookup($name as xs:QName, $arity as xs:integer) as function(*)? external; (: §16.1.1 :)
declare function fn:function-name($func as function(*)) as xs:QName? external; (: §16.1.2:)
declare function fn:generate-id() as xs:string external; (: §14.7.4 :)
declare function fn:generate-id($arg as node()?) as xs:string external; (: §14.7.4 :)
declare function fn:has-children() as xs:boolean external; (: §13.10 :)
declare function fn:has-children($node as node()?) as xs:boolean external; (: §13.10 :)
declare function fn:head($arg as item()*) as item()? external; (: §14.1.4 :)
declare function fn:hours-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.4 :)
declare function fn:hours-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.4 :)
declare function fn:hours-from-time($arg as xs:time?) as xs:integer? external; (: §9.5.12 :)
declare function fn:id($arg as xs:string*) as element()* external; (: §14.7.1 :)
declare function fn:id($arg as xs:string*, $node as node()) as element()* external; (: §14.7.1 :)
declare function fn:idref($arg as xs:string*) as node()* external; (: §14.7.3 :)
declare function fn:idref($arg as xs:string*, $node as node()) as node()* external; (: §14.7.3 :)
declare function fn:implicit-timezone() as xs:dayTimeDuration external; (: §15.6 :)
declare function fn:in-scope-prefixes($element as element()) as xs:string* external; (: §10.2.6 :)
declare function fn:index-of($seq as xs:anyAtomicType*, $search as xs:anyAtomicType) as xs:integer* external; (: §14.2.2 :)
declare function fn:index-of($seq as xs:anyAtomicType*, $search as xs:anyAtomicType, $collation as xs:string) as xs:integer* external; (: §14.2.2 :)
declare function fn:innermost($nodes as node()*) as node()* external; (: §13.11 :)
declare function fn:insert-before($target as item()*, $position as xs:integer, $inserts as item()*) as item()* external; (: §14.1.6 :)
declare function fn:iri-to-uri($iri as xs:string?) as xs:string external; (: §6.3 :)
declare function fn:lang($testlang as xs:string?) as xs:boolean external; (: §13.4 :)
declare function fn:lang($testlang as xs:string?, $node as node()) as xs:boolean external; (: §13.4 :)
declare function fn:last() as xs:integer external; (: §15.2 :)
declare function fn:local-name() as xs:string external; (: §13.2 :)
declare function fn:local-name($arg as node()?) as xs:string external; (: §13.2 :)
declare function fn:local-name-from-QName($arg as xs:QName?) as xs:NCName? external; (: §10.2.3 :)
declare function fn:lower-case($arg as xs:string?) as xs:string external; (: §5.4.8 :)
declare function fn:matches($input as xs:string?, $pattern as xs:string) as xs:boolean external; (: §5.6.2 :)
declare function fn:matches($input as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:boolean external; (: §5.6.2 :)
declare function fn:max($arg as xs:anyAtomicType*) as xs:anyAtomicType? external; (: §14.5.3 :)
declare function fn:max($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType? external; (: §14.5.3 :)
declare function fn:min($arg as xs:anyAtomicType*) as xs:anyAtomicType? external; (: §14.5.4 :)
declare function fn:min($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType? external; (: §14.5.4 :)
declare function fn:minutes-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.5 :)
declare function fn:minutes-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.5 :)
declare function fn:minutes-from-time($arg as xs:time?) as xs:integer? external; (: §9.5.13 :)
declare function fn:month-from-date($arg as xs:date?) as xs:integer? external; (: §9.5.9 :)
declare function fn:month-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.2 :)
declare function fn:months-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.2 :)
declare function fn:name() as xs:string external; (: §13.1 :)
declare function fn:name($arg as node()?) as xs:string external; (: §13.1 :)
declare function fn:namespace-uri() as xs:anyURI external; (: §13.3 :)
declare function fn:namespace-uri($arg as node()?) as xs:anyURI external; (: §13.3 :)
declare function fn:namespace-uri-for-prefix($prefix as xs:string?, $element as element()) as xs:anyURI? external; (: §10.2.5 :)
declare function fn:namespace-uri-from-QName($arg as xs:QName?) as xs:anyURI? external; (: §10.2.4 :)
declare function fn:nilled() as xs:boolean external; (: §2.2 :)
declare function fn:nilled($arg as node()?) as xs:boolean? external; (: §2.2 :)
declare function fn:node-name() as xs:QName? external; (: §2.1 :)
declare function fn:node-name($arg as node()?) as xs:QName? external; (: §2.1 :)
declare function fn:normalize-space() as xs:string external; (: §5.4.5 :)
declare function fn:normalize-space($arg as xs:string?) as xs:string external; (: §5.4.5 :)
declare function fn:normalize-unicode($arg as xs:string?) as xs:string external; (: §5.4.6 :)
declare function fn:normalize-unicode($arg as xs:string?, $normalizationForm as xs:string) as xs:string external; (: §5.4.6 :)
declare function fn:not($arg as item()*) as xs:boolean external; (: §7.3.1 :)
declare function fn:number() as xs:double external; (: §4.5.1 :)
declare function fn:number($arg as xs:anyAtomicType?) as xs:double external; (: §4.5.1 :)
declare function fn:one-or-more($arg as item()*) as item()+ external; (: §14.3.2 :)
declare function fn:outermost($nodes as node()*) as node()* external; (: §13.12 :)
declare function fn:parse-xml($arg as xs:string?) as document-node(element(*))? external; (: §14.9.1 :)
declare function fn:parse-xml-fragment($arg as xs:string?) as document-node()? external; (: §14.9.2 :)
declare function fn:path() as xs:string? external; (: §13.9 :)
declare function fn:path($arg as node()?) as xs:string? external; (: §13.9 :)
declare function fn:position() as xs:integer external; (: §15.1 :)
declare function fn:prefix-from-QName($arg as xs:QName?) as xs:NCName? external; (: §10.2.2 :)
declare function fn:remove($target as item()*, $position as xs:integer) as item()* external; (: §14.1.7 :)
declare function fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string) as xs:string external; (: §5.6.3 :)
declare function fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string, $flags as xs:string) as xs:string external; (: §5.6.3 :)
declare function fn:resolve-QName($qname as xs:string?, $element as element()) as xs:QName? external; (: §10.1.1 :)
declare function fn:resolve-uri($relative as xs:string?) as xs:anyURI? external; (: §6.1 :)
declare function fn:resolve-uri($relative as xs:string?, $base as xs:string) as xs:anyURI? external; (: §6.1 :)
declare function fn:reverse($arg as item()*) as item()* external; (: §14.1.8 :)
declare function fn:root() as node() external; (: §13.8 :)
declare function fn:root($arg as node()?) as node()? external; (: §13.8 :)
declare function fn:round($arg as xs:numeric?) as xs:numeric? external; (: §4.4.4 :)
declare function fn:round($arg as xs:numeric?, $precision as xs:integer) as xs:numeric? external; (: §4.4.4 :)
declare function fn:round-half-to-even($arg as xs:numeric?) as xs:numeric? external; (: §4.4.5 :)
declare function fn:round-half-to-even($arg as xs:numeric?, $precision as xs:integer) as xs:numeric? external; (: §4.4.5 :)
declare function fn:seconds-from-dateTime($arg as xs:dateTime?) as xs:decimal? external; (: §9.5.6 :)
declare function fn:seconds-from-duration($arg as xs:duration?) as xs:decimal? external; (: §8.3.6 :)
declare function fn:seconds-from-time($arg as xs:time?) as xs:decimal? external; (: §9.5.14 :)
declare function fn:serialize($arg as item()*) as xs:string external; (: §14.9.3 :)
declare function fn:serialize($arg as item()*, $params as element(output:serialization-parameters)?) as xs:string external; (: §14.9.3 :)
declare function fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean external; (: §5.5.2 :)
declare function fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean external; (: §5.5.2 :)
declare function fn:static-base-uri() as xs:anyURI? external; (: §15.8 :)
declare function fn:string() as xs:string external; (: §2.3 :)
declare function fn:string($arg as item()?) as xs:string external; (: §2.3 :)
declare function fn:string-join($arg1 as xs:string*) as xs:string external; (: §5.4.2 :)
declare function fn:string-join($arg1 as xs:string*, $arg2 as xs:string) as xs:string external; (: §5.4.2 :)
declare function fn:string-length() as xs:integer external; (: §5.4.4 :)
declare function fn:string-length($arg as xs:string?) as xs:integer external; (: §5.4.4 :)
declare function fn:string-to-codepoints($arg as xs:string?) as xs:integer* external; (: §5.2.2 :)
declare function fn:subsequence($sourceSeq as item()*, $startingLoc as xs:double) as item()* external; (: §14.1.9 :)
declare function fn:subsequence($sourceSeq as item()*, $startingLoc as xs:double, $length as xs:double) as item()* external; (: §14.1.9 :)
declare function fn:substring($sourceString as xs:string?, $start as xs:double) as xs:string external; (: §5.4.3 :)
declare function fn:substring($sourceString as xs:string?, $start as xs:double, $length as xs:double) as xs:string external; (: §5.4.3 :)
declare function fn:substring-after($arg1 as xs:string?, $arg2 as xs:string?) as xs:string external; (: §5.5.5 :)
declare function fn:substring-after($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:string external; (: §5.5.5 :)
declare function fn:substring-before($arg1 as xs:string?, $arg2 as xs:string?) as xs:string external; (: §5.5.4 :)
declare function fn:substring-before($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:string external; (: §5.5.4 :)
declare function fn:sum($arg as xs:anyAtomicType*) as xs:anyAtomicType external; (: §14.5.5 :)
declare function fn:sum($arg as xs:anyAtomicType*, $zero as xs:anyAtomicType?) as xs:anyAtomicType? external; (: §14.5.5 :)
declare function fn:tail($arg as item()*) as item()? external; (: §14.1.5 :)
declare function fn:timezone-from-date($arg as xs:date?) as xs:dayTimeDuration? external; (: §9.5.11 :)
declare function fn:timezone-from-dateTime($arg as xs:dateTime?) as xs:dayTimeDuration? external; (: §9.5.7 :)
declare function fn:timezone-from-time($arg as xs:time?) as xs:dayTimeDuration? external; (: §9.5.15 :)
declare function fn:tokenize($input as xs:string?, $pattern as xs:string) as xs:string* external; (: §5.6.4 :)
declare function fn:tokenize($input as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:string* external; (: §5.6.4 :)
declare function fn:trace($value as item()*, $label as xs:string) as item()* external; (: §3.2 :)
declare function fn:translate($arg as xs:string?, $mapString as xs:string, $transString as xs:string) as xs:string external; (: §5.4.9 :)
declare function fn:true() as xs:boolean external; (: §7.1.1 :)
declare function fn:unordered($sourceSeq as item()*) as item()* external; (: §14.1.10 :)
declare function fn:unparsed-text($href as xs:string?) as xs:string? external; (: §14.8.5 :)
declare function fn:unparsed-text($href as xs:string?, $encoding as xs:string) as xs:string? external; (: §14.8.5 :)
declare function fn:unparsed-text-available($href as xs:string?) as xs:string? external; (: §14.8.7 :)
declare function fn:unparsed-text-available($href as xs:string?, $encoding as xs:string) as xs:string? external; (: §14.8.7 :)
declare function fn:unparsed-text-lines($href as xs:string?) as xs:string? external; (: §14.8.6 :)
declare function fn:unparsed-text-lines($href as xs:string?, $encoding as xs:string) as xs:string? external; (: §14.8.6 :)
declare function fn:upper-case($arg as xs:string?) as xs:string external; (: §5.4.7 :)
declare function fn:uri-collection() as xs:anyURI* external; (: §14.8.4 :)
declare function fn:uri-collection($arg as xs:string?) as xs:anyURI* external; (: §14.8.4 :)
declare function fn:year-from-date($arg as xs:date?) as xs:integer? external; (: §9.5.8 :)
declare function fn:year-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.1 :)
declare function fn:years-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.1 :)
declare function fn:zero-or-one($arg as item()*) as item()? external; (: §14.3.1 :)

(: XPath and XQuery Functions and Operators 3.0
 : W3C Recommendation 08 April 2014
 :
 : Reference: https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/
 :)
xquery version "3.0";

module  namespace fn = "http://www.w3.org/2005/xpath-functions";
declare namespace xs = "http://www.w3.org/2001/XMLSchema";

declare function fn:QName($paramURI as xs:string?, $paramQName as xs:string) as xs:QName external; (: §10.1.2 :)
declare function fn:abs($arg as xs:xs:numeric?) as xs:numeric? external; (: §4.4.1 :)
declare function fn:adjust-date-to-timezone($arg as xs:date?) as xs:date? external; (: §9.6.2 :)
declare function fn:adjust-date-to-timezone($arg as xs:date?, $timezone as xs:dayTimeDuration?) as xs:date? external; (: §9.6.2 :)
declare function fn:adjust-dateTime-to-timezone($arg as xs:dateTime?) as xs:dateTime? external; (: §9.6.1 :)
declare function fn:adjust-dateTime-to-timezone($arg as xs:dateTime?, $timezone as xs:dayTimeDuration?) as xs:dateTime? external; (: §9.6.1 :)
declare function fn:adjust-time-to-timezone($arg as xs:time?) as xs:time? external; (: §9.6.3 :)
declare function fn:adjust-time-to-timezone($arg as xs:time?, $timezone as xs:dayTimeDuration?) as xs:time? external; (: §9.6.3 :)
declare function fn:analyze-string($input as xs:string?, $pattern as xs:string) as element(fn:analyze-string-result) external; (: §5.6.5 :)
declare function fn:analyze-string($input as xs:string?, $pattern as xs:string, $flags as xs:string) as element(fn:analyze-string-result) external; (: §5.6.5 :)
declare function fn:available-environment-variables() as xs:string* external; (: §14.8.9 :)
declare function fn:avg($arg as xs:anyAtomicType*) as xs:anyAtomicType? external; (: §14.5.2 :)
declare function fn:base-uri() as xs:anyURI? external; (: §2.5 :)
declare function fn:base-uri($arg as node()?) as xs:anyURI? external; (: §2.5 :)
declare function fn:boolean($arg as item()*) as xs:boolean external; (: §7.3.1 :)
declare function fn:ceiling($arg as xs:numeric?) as xs:numeric? external; (: §4.4.2 :)
declare function fn:codepoint-equal($comparand1 as xs:string?, $comparand2 as xs:string?) as xs:boolean? external; (: §5.3.5 :)
declare function fn:codepoints-to-string($arg as xs:integer*) as xs:string external; (: §5.2.1 :)
declare function fn:collection() as node()* external; (: §14.8.3 :)
declare function fn:collection($arg as xs:string?) as node()* external; (: §14.8.3 :)
declare function fn:compare($comparand1 as xs:string?, $comparand2 as xs:string?) as xs:integer? external; (: §5.3.4 :)
declare function fn:compare($comparand1 as xs:string?, $comparand2 as xs:string?, $collation as xs:string) as xs:integer? external; (: §5.3.4 :)
declare %variadic("xs:anyAtomicType?") function fn:concat($arg1 as xs:anyAtomicType?, $arg2 as xs:anyAtomicType?) as xs:string external; (: §5.4.1 :)
declare function fn:contains($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean external; (: §5.5.1 :)
declare function fn:contains($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean external; (: §5.5.1 :)
declare function fn:count($arg as item()*) as xs:integer external; (: §14.5.1 :)
declare function fn:current-date() as xs:date external; (: §15.4 :)
declare function fn:current-dateTime() as xs:dateTime external; (: §15.3 :)
declare function fn:current-time() as xs:time external; (: §15.5 :)
declare function fn:data() as xs:anyAtomicType* external; (: §2.4 :)
declare function fn:data($arg as item()*) as xs:anyAtomicType* external; (: §2.4 :)
declare function fn:dateTime($arg1 as xs:date?, $arg2 as xs:time?) as xs:dateTime? external; (: §9.3.1 :)
declare function fn:day-from-date($arg as xs:date?) as xs:integer? external; (: §9.5.10 :)
declare function fn:day-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.3 :)
declare function fn:days-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.3 :)
declare function fn:deep-equal($parameter1 as item()*, $parameter2 as item()*) as xs:boolean external; (: §14.2.3 :)
declare function fn:deep-equal($parameter1 as item()*, $parameter2 as item()*, $collation as string) as xs:boolean external; (: §14.2.3 :)
declare function fn:default-collation() as xs:string external; (: §15.7 :)
declare function fn:distinct-values($arg as xs:anyAtomicType*) as xs:anyAtomicType* external; (: §14.2.1 :)
declare function fn:distinct-values($arg as xs:anyAtomicType*, $collation as xs:string) as xs:anyAtomicType* external; (: §14.2.1 :)
declare function fn:doc($uri as xs:string?) as document-node()? external; (: §14.8.1 :)
declare function fn:doc-available($uri as xs:string?) as xs:boolean external; (: §14.8.2 :)
declare function fn:document-uri() as xs:anyURI? external; (: §2.6 :)
declare function fn:document-uri($arg as node()?) as xs:anyURI? external; (: §2.6 :)
declare function fn:element-with-id($arg as xs:string*) as element()* external; (: §14.7.2 :)
declare function fn:element-with-id($arg as xs:string*, $node as node()) as element()* external; (: §14.7.2 :)
declare function fn:empty($arg as item()*) as xs:boolean external; (: §14.1.2 :)
declare function fn:encode-for-uri($uri-part as xs:string?) as xs:string external; (: §6.2 :)
declare function fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean external; (: §5.5.3 :)
declare function fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean external; (: §5.5.3 :)
declare function fn:environment-variable($name as xs:string) as xs:string? external; (: §14.8.8 :)
declare function fn:error() as none external; (: §3.1 :)
declare function fn:error($error as xs:QName) as none external; (: §3.1 :)
declare function fn:error($error as xs:QName?, $description as xs:string) as none external; (: §3.1 :)
declare function fn:error($error as xs:QName?, $description as xs:string, $error-object as item()*) as none external; (: §3.1 :)
declare function fn:escape-html-uri($uri as xs:string?) as xs:string external; (: §6.4 :)
declare function fn:exactly-one($arg as item()*) as item() external; (: §14.3.3 :)
declare function fn:exists($arg as item()*) as xs:boolean external; (: §14.1.3 :)
declare function fn:false() as xs:boolean external; (: §7.1.2 :)
declare function fn:filter($seq as item()*, $f as function(item()) as xs:boolean) as item()* external; (: §16.2.2 :)
declare function fn:floor($arg as xs:numeric?) as xs:numeric? external; (: §4.4.3 :)
declare function fn:fold-left($seq as item()*, $zero as item()*, $f as function(item()*, item()) as item()*) as item()* external; (: §16.2.3 :)
declare function fn:fold-right($seq as item()*, $zero as item()*, $f as function(item()*, item()) as item()*) as item()* external; (: §16.2.4 :)
declare function fn:for-each($seq as item()*, $f as function(item()) as item()*) as item()* external; (: §16.2.1 :)
declare function fn:for-each-pair($seq1 as item()*, $seq2 as item()*, $f as function(item(), item()) as item()*) as item()* external; (: §16.2.5 :)
declare function fn:format-date($value as xs:date?, $picture as xs:string) as xs:string? external; (: §9.8.2 :)
declare function fn:format-date($value as xs:date?, $picture as xs:string, $language as xs:string?, $calendar as xs:string?, $place as xs:string?) as xs:string? external; (: §9.8.2 :)
declare function fn:format-dateTime($value as xs:dateTime?, $picture as xs:string) as xs:string? external; (: §9.8.1 :)
declare function fn:format-dateTime($value as xs:dateTime?, $picture as xs:string, $language as xs:string?, $calendar as xs:string?, $place as xs:string?) as xs:string? external; (: §9.8.1 :)
declare function fn:format-integer($value as xs:integer?, $picture as xs:string) as xs:string external; (: §4.6.1 :)
declare function fn:format-integer($value as xs:integer?, $picture as xs:string, $lang as xs:string?) as xs:string external (: §4.6.1 :)
declare function fn:format-number($value as xs:numeric?, $picture as xs:string) as xs:string external; (: §4.7.2 :)
declare function fn:format-number($value as xs:numeric?, $picture as xs:string, $decimal-format-name as xs:string?) as xs:string external; (: §4.7.2 :)
declare function fn:format-time($value as xs:time?, $picture as xs:string) as xs:string? external; (: §9.8.3 :)
declare function fn:format-time($value as xs:time?, $picture as xs:string, $language as xs:string?, $calendar as xs:string?, $place as xs:string?) as xs:string? external; (: §9.8.3 :)
declare function fn:function-arity($func as function(*)) as xs:integer external; (: §16.1.3 :)
declare function fn:function-lookup($name as xs:QName, $arity as xs:integer) as function(*)? external; (: §16.1.1 :)
declare function fn:function-name($func as function(*)) as xs:QName? external; (: §16.1.2:)
declare function fn:generate-id() as xs:string external; (: §14.7.4 :)
declare function fn:generate-id($arg as node()?) as xs:string external; (: §14.7.4 :)
declare function fn:has-children() as xs:boolean external; (: §13.10 :)
declare function fn:has-children($node as node()?) as xs:boolean external; (: §13.10 :)
declare function fn:head($arg as item()*) as item()? external; (: §14.1.4 :)
declare function fn:hours-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.4 :)
declare function fn:hours-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.4 :)
declare function fn:hours-from-time($arg as xs:time?) as xs:integer? external; (: §9.5.12 :)
declare function fn:id($arg as xs:string*) as element()* external; (: §14.7.1 :)
declare function fn:id($arg as xs:string*, $node as node()) as element()* external; (: §14.7.1 :)
declare function fn:idref($arg as xs:string*) as node()* external; (: §14.7.3 :)
declare function fn:idref($arg as xs:string*, $node as node()) as node()* external; (: §14.7.3 :)
declare function fn:implicit-timezone() as xs:dayTimeDuration external; (: §15.6 :)
declare function fn:in-scope-prefixes($element as element()) as xs:string* external; (: §10.2.6 :)
declare function fn:index-of($seq as xs:anyAtomicType*, $search as xs:anyAtomicType) as xs:integer* external; (: §14.2.2 :)
declare function fn:index-of($seq as xs:anyAtomicType*, $search as xs:anyAtomicType, $collation as xs:string) as xs:integer* external; (: §14.2.2 :)
declare function fn:innermost($nodes as node()*) as node()* external; (: §13.11 :)
declare function fn:insert-before($target as item()*, $position as xs:integer, $inserts as item()*) as item()* external; (: §14.1.6 :)
declare function fn:iri-to-uri($iri as xs:string?) as xs:string external; (: §6.3 :)
declare function fn:lang($testlang as xs:string?) as xs:boolean external; (: §13.4 :)
declare function fn:lang($testlang as xs:string?, $node as node()) as xs:boolean external; (: §13.4 :)
declare function fn:last() as xs:integer external; (: §15.2 :)
declare function fn:local-name() as xs:string external; (: §13.2 :)
declare function fn:local-name($arg as node()?) as xs:string external; (: §13.2 :)
declare function fn:local-name-from-QName($arg as xs:QName?) as xs:NCName? external; (: §10.2.3 :)
declare function fn:lower-case($arg as xs:string?) as xs:string external; (: §5.4.8 :)
declare function fn:matches($input as xs:string?, $pattern as xs:string) as xs:boolean external; (: §5.6.2 :)
declare function fn:matches($input as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:boolean external; (: §5.6.2 :)
declare function fn:max($arg as xs:anyAtomicType*) as xs:anyAtomicType? external; (: §14.5.3 :)
declare function fn:max($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType? external; (: §14.5.3 :)
declare function fn:min($arg as xs:anyAtomicType*) as xs:anyAtomicType? external; (: §14.5.4 :)
declare function fn:min($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType? external; (: §14.5.4 :)
declare function fn:minutes-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.5 :)
declare function fn:minutes-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.5 :)
declare function fn:minutes-from-time($arg as xs:time?) as xs:integer? external; (: §9.5.13 :)
declare function fn:month-from-date($arg as xs:date?) as xs:integer? external; (: §9.5.9 :)
declare function fn:month-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.2 :)
declare function fn:months-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.2 :)
declare function fn:name() as xs:string external; (: §13.1 :)
declare function fn:name($arg as node()?) as xs:string external; (: §13.1 :)
declare function fn:namespace-uri() as xs:anyURI external; (: §13.3 :)
declare function fn:namespace-uri($arg as node()?) as xs:anyURI external; (: §13.3 :)
declare function fn:namespace-uri-for-prefix($prefix as xs:string?, $element as element()) as xs:anyURI? external; (: §10.2.5 :)
declare function fn:namespace-uri-from-QName($arg as xs:QName?) as xs:anyURI? external; (: §10.2.4 :)
declare function fn:nilled() as xs:boolean external; (: §2.2 :)
declare function fn:nilled($arg as node()?) as xs:boolean? external; (: §2.2 :)
declare function fn:node-name() as xs:QName? external; (: §2.1 :)
declare function fn:node-name($arg as node()?) as xs:QName? external; (: §2.1 :)
declare function fn:normalize-space() as xs:string external; (: §5.4.5 :)
declare function fn:normalize-space($arg as xs:string?) as xs:string external; (: §5.4.5 :)
declare function fn:normalize-unicode($arg as xs:string?) as xs:string external; (: §5.4.6 :)
declare function fn:normalize-unicode($arg as xs:string?, $normalizationForm as xs:string) as xs:string external; (: §5.4.6 :)
declare function fn:not($arg as item()*) as xs:boolean external; (: §7.3.1 :)
declare function fn:number() as xs:double external; (: §4.5.1 :)
declare function fn:number($arg as xs:anyAtomicType?) as xs:double external; (: §4.5.1 :)
declare function fn:one-or-more($arg as item()*) as item()+ external; (: §14.3.2 :)
declare function fn:outermost($nodes as node()*) as node()* external; (: §13.12 :)
declare function fn:parse-xml($arg as xs:string?) as document-node(element(*))? external; (: §14.9.1 :)
declare function fn:parse-xml-fragment($arg as xs:string?) as document-node()? external; (: §14.9.2 :)
declare function fn:path() as xs:string? external; (: §13.9 :)
declare function fn:path($arg as node()?) as xs:string? external; (: §13.9 :)
declare function fn:position() as xs:integer external; (: §15.1 :)
declare function fn:prefix-from-QName($arg as xs:QName?) as xs:NCName? external; (: §10.2.2 :)
declare function fn:remove($target as item()*, $position as xs:integer) as item()* external; (: §14.1.7 :)
declare function fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string) as xs:string external; (: §5.6.3 :)
declare function fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string, $flags as xs:string) as xs:string external; (: §5.6.3 :)
declare function fn:resolve-QName($qname as xs:string?, $element as element()) as xs:QName? external; (: §10.1.1 :)
declare function fn:resolve-uri($relative as xs:string?) as xs:anyURI? external; (: §6.1 :)
declare function fn:resolve-uri($relative as xs:string?, $base as xs:string) as xs:anyURI? external; (: §6.1 :)
declare function fn:reverse($arg as item()*) as item()* external; (: §14.1.8 :)
declare function fn:root() as node() external; (: §13.8 :)
declare function fn:root($arg as node()?) as node()? external; (: §13.8 :)
declare function fn:round($arg as xs:numeric?) as xs:numeric? external; (: §4.4.4 :)
declare function fn:round($arg as xs:numeric?, $precision as xs:integer) as xs:numeric? external; (: §4.4.4 :)
declare function fn:round-half-to-even($arg as xs:numeric?) as xs:numeric? external; (: §4.4.5 :)
declare function fn:round-half-to-even($arg as xs:numeric?, $precision as xs:integer) as xs:numeric? external; (: §4.4.5 :)
declare function fn:seconds-from-dateTime($arg as xs:dateTime?) as xs:decimal? external; (: §9.5.6 :)
declare function fn:seconds-from-duration($arg as xs:duration?) as xs:decimal? external; (: §8.3.6 :)
declare function fn:seconds-from-time($arg as xs:time?) as xs:decimal? external; (: §9.5.14 :)
declare function fn:serialize($arg as item()*) as xs:string external; (: §14.9.3 :)
declare function fn:serialize($arg as item()*, $params as element(output:serialization-parameters)?) as xs:string external; (: §14.9.3 :)
declare function fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean external; (: §5.5.2 :)
declare function fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean external; (: §5.5.2 :)
declare function fn:static-base-uri() as xs:anyURI? external; (: §15.8 :)
declare function fn:string() as xs:string external; (: §2.3 :)
declare function fn:string($arg as item()?) as xs:string external; (: §2.3 :)
declare function fn:string-join($arg1 as xs:string*) as xs:string external; (: §5.4.2 :)
declare function fn:string-join($arg1 as xs:string*, $arg2 as xs:string) as xs:string external; (: §5.4.2 :)
declare function fn:string-length() as xs:integer external; (: §5.4.4 :)
declare function fn:string-length($arg as xs:string?) as xs:integer external; (: §5.4.4 :)
declare function fn:string-to-codepoints($arg as xs:string?) as xs:integer* external; (: §5.2.2 :)
declare function fn:subsequence($sourceSeq as item()*, $startingLoc as xs:double) as item()* external; (: §14.1.9 :)
declare function fn:subsequence($sourceSeq as item()*, $startingLoc as xs:double, $length as xs:double) as item()* external; (: §14.1.9 :)
declare function fn:substring($sourceString as xs:string?, $start as xs:double) as xs:string external; (: §5.4.3 :)
declare function fn:substring($sourceString as xs:string?, $start as xs:double, $length as xs:double) as xs:string external; (: §5.4.3 :)
declare function fn:substring-after($arg1 as xs:string?, $arg2 as xs:string?) as xs:string external; (: §5.5.5 :)
declare function fn:substring-after($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:string external; (: §5.5.5 :)
declare function fn:substring-before($arg1 as xs:string?, $arg2 as xs:string?) as xs:string external; (: §5.5.4 :)
declare function fn:substring-before($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:string external; (: §5.5.4 :)
declare function fn:sum($arg as xs:anyAtomicType*) as xs:anyAtomicType external; (: §14.5.5 :)
declare function fn:sum($arg as xs:anyAtomicType*, $zero as xs:anyAtomicType?) as xs:anyAtomicType? external; (: §14.5.5 :)
declare function fn:tail($arg as item()*) as item()? external; (: §14.1.5 :)
declare function fn:timezone-from-date($arg as xs:date?) as xs:dayTimeDuration? external; (: §9.5.11 :)
declare function fn:timezone-from-dateTime($arg as xs:dateTime?) as xs:dayTimeDuration? external; (: §9.5.7 :)
declare function fn:timezone-from-time($arg as xs:time?) as xs:dayTimeDuration? external; (: §9.5.15 :)
declare function fn:tokenize($input as xs:string?, $pattern as xs:string) as xs:string* external; (: §5.6.4 :)
declare function fn:tokenize($input as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:string* external; (: §5.6.4 :)
declare function fn:trace($value as item()*, $label as xs:string) as item()* external; (: §3.2 :)
declare function fn:translate($arg as xs:string?, $mapString as xs:string, $transString as xs:string) as xs:string external; (: §5.4.9 :)
declare function fn:true() as xs:boolean external; (: §7.1.1 :)
declare function fn:unordered($sourceSeq as item()*) as item()* external; (: §14.1.10 :)
declare function fn:unparsed-text($href as xs:string?) as xs:string? external; (: §14.8.5 :)
declare function fn:unparsed-text($href as xs:string?, $encoding as xs:string) as xs:string? external; (: §14.8.5 :)
declare function fn:unparsed-text-available($href as xs:string?) as xs:string? external; (: §14.8.7 :)
declare function fn:unparsed-text-available($href as xs:string?, $encoding as xs:string) as xs:string? external; (: §14.8.7 :)
declare function fn:unparsed-text-lines($href as xs:string?) as xs:string? external; (: §14.8.6 :)
declare function fn:unparsed-text-lines($href as xs:string?, $encoding as xs:string) as xs:string? external; (: §14.8.6 :)
declare function fn:upper-case($arg as xs:string?) as xs:string external; (: §5.4.7 :)
declare function fn:uri-collection() as xs:anyURI* external; (: §14.8.4 :)
declare function fn:uri-collection($arg as xs:string?) as xs:anyURI* external; (: §14.8.4 :)
declare function fn:year-from-date($arg as xs:date?) as xs:integer? external; (: §9.5.8 :)
declare function fn:year-from-dateTime($arg as xs:dateTime?) as xs:integer? external; (: §9.5.1 :)
declare function fn:years-from-duration($arg as xs:duration?) as xs:integer? external; (: §8.3.1 :)
declare function fn:zero-or-one($arg as item()*) as item()? external; (: §14.3.1 :)


[testsuite] Extended XQuery test suite library to support serialization parameters and test for expected errors.

svn path=/trunk/eXist/; revision=13756

xquery version "1.0";

module namespace t="http://exist-db.org/xquery/testing";

import module namespace xdb="http://exist-db.org/xquery/xmldb";
import module namespace xdiff="http://exist-db.org/xquery/xmldiff"
at "java:org.exist.xquery.modules.xmldiff.XmlDiffModule";

declare function t:setup-action($action) {
    typeswitch ($action)
        case element(create-collection) return
            xdb:create-collection($action/@parent, $action/@name)
        case element(store) return
			t:store($action)
		case element(store-files) return
            t:store-files($action)
        case element(remove-collection) return
            xdb:remove($action/@collection)
        case element(remove-document) return
            xdb:remove($action/@collection, $action/@name)
        default return
            ()
};

declare function t:store($action as element(store)) {
    let $type := if ($action/@type) then $action/@type/string() else "application/xml"
    let $data :=
		if ($action/*) then
			$action/*[1]
		else
			$action/string()
	return
        xdb:store($action/@collection, $action/@name, $data, $type)
};

declare function t:store-files($action as element(store-files)) {
    let $type := if ($action/@type) then $action/@type/string() else "application/xml"
    return
        xdb:store-files-from-pattern($action/@collection, $action/@dir, $action/@pattern, $type)
};

declare function t:setup($setup as element(setup)) {
    for $action in $setup/*
    return
        t:setup-action($action)
};

declare function t:tearDown($tearDown as element(tearDown)?) {
    for $action in $tearDown/*
    return
        t:setup-action($action)
};

declare function t:declare-variable($var as element(variable)) as item()? {
    let $children := $var/*
    return
        if (empty($children)) then
            string-join($var/node(), '')
        else
            util:serialize($children, ())
};

declare function t:init-prolog($test as element(test)) {
	let $imports := $test/../imports
	let $vars :=
	    string-join(
        	for $var in $test/../variable
        	return
        	    concat("declare variable $", $var/@name, " := ", t:declare-variable($var), ";"),
            ""
        )
	return
		string-join(($imports, $vars, $test/../functions), '')
};

declare function t:test($result as item()*) {
    if ($result instance of xs:boolean) then
        $result
    else
        exists($result)
};

declare function t:run-test($test as element(test), $count as xs:integer) {
	let $context := t:init-prolog($test)
	let $null :=
	   if ($test/@trace eq 'yes') then
	       (system:clear-trace(), system:enable-tracing(true(), false()))
       else ()
    let $highlight-option := concat("highlight-matches=",
          if ($test/expected//@*[matches(., '^(\|{3}).*\1$')] and $test/expected//exist:match) then "both"
          else if ($test/expected//@*[matches(., '^(\|{3}).*\1$')]) then "attributes"
          else if ($test/expected//exist:match) then "elements"
          else "none"
          )
    let $serialize-options :=
      let $decls := ($test/../*[name() ne 'test']|$test/code)[matches(., 'declare[\- ]option(\(("|')|\s+)exist:serialize(\2,)?\s+("|').*?\4[;)]')]
      let $ops1 := $decls/replace(., "declare[\- ]option(\(("|')|\s+)exist:serialize(\2,)?\s+("|')(.*?)\4[;)]", "_|$5_")
      let $ops2 :=
        for $a in $ops1
        for $b in tokenize($a, '_')[starts-with(., '|')]
        return tokenize(substring-after($b, '|'), '\s+')
      return if (count($ops2[matches(., 'highlight-matches')]))
        then string-join($ops2, ' ')
        else string-join(($ops2, $highlight-option), ' ')
    let $queryOutput :=
		util:catch("*",
			util:eval(concat($context, $test/code/string())),
			<error>Compilation error: {$util:exception-message}</error>
		)
	let $output := if ($test/@trace eq 'yes') then system:trace() else $queryOutput
    let $expanded :=
        if ($output instance of element(error)) then
            $output
        else if ($test/@serialize) then
            let $options := $test/@serialize
            let $serialized :=
                util:catch("*",
                    for $x in $output
                    return
                        util:serialize($x, $options),
                    <error>Serialization error: {$util:exception-message}</error>
                )
            return
                if ($serialized instance of element(error)) then
                    $serialized
                else
                    normalize-space(string-join($serialized, ' '))
		else if ($output instance of node()) then
        	util:expand($output, $serialize-options)
		else
			$output
    let $expected :=
        if ($test/@output eq 'text') then
            data($test/expected)
        else $test/expected/node()
    let $OK :=
        if ($test/error) then
            $expanded instance of element(error) and contains($expanded, $test/error)
        else if ($test/xpath) then
            t:test(t:xpath($output, $test/xpath))
        else if ($test/@output eq 'text') then
            let $asString :=
                if ($test/@serialize) then
                    $expanded
                else
                    normalize-space(string-join(for $x in $output return string($x),' '))
            return
                $asString eq normalize-space($expected)
        else
            let $xn := t:normalize($expanded)
            let $en := t:normalize($expected)
            return
        	   deep-equal($xn, $en)
    return
        <test n="{$count}" pass="{$OK}">
        {
            if (not($OK)) then
                ($test/task, $test/expected, <result>{$expanded}</result>)
            else
                ()
        }
        </test>
};

declare function t:normalize($nodes as node()*) {
	for $node in $nodes return t:normalize-node($node)
};

declare function t:normalize-node($node as node()) {
	typeswitch ($node)
		case element() return
			element { node-name($node) } {
				$node/@*, for $child in $node/node() return t:normalize-node($child)
			}
		case text() return
			let $norm := normalize-space($node)
			return
				if (string-length($norm) eq 0) then () else $node
		default return
			$node
};

declare function t:xpath($output as item()*, $xpath as element()) {
    for $prefix in in-scope-prefixes($xpath)
    let $nsuri := namespace-uri-for-prefix($prefix, $xpath)
    return
        if ($prefix != 'xml') then
            util:declare-namespace($prefix, $nsuri)
        else (),
    let $expr := $xpath/string()
    return
        if (matches($expr, "^\s*/")) then
            util:eval(concat("$output", $expr))
        else
            util:eval($expr)
};

declare function t:run-testSet($set as element(TestSet)) {
    let $copy := util:expand($set)
    let $null := t:setup($copy/setup)
    let $result := util:expand(
           <TestSet>
           {$copy/testName}
           {$copy/description}
           {
               for $test at $p in $copy/test
               return
                   t:run-test($test, $p)
           }
           </TestSet>
        )
    let $null := t:tearDown($copy/tearDown)
    return $result
};

declare function t:format-testResult($result) {
    let $formatted :=

{$result/testName}

{$result/description/p}

          {for $test in $result//test
           return

                    {$test/@n/.}
                    {$test/@pass/.}

          }

     return
        $formatted
};

xquery version "1.0";

module namespace t="http://exist-db.org/xquery/testing";

import module namespace xdb="http://exist-db.org/xquery/xmldb";
import module namespace xdiff="http://exist-db.org/xquery/xmldiff"
at "java:org.exist.xquery.modules.xmldiff.XmlDiffModule";

declare function t:setup-action($action) {
    typeswitch ($action)
        case element(create-collection) return
            xdb:create-collection($action/@parent, $action/@name)
        case element(store) return
			t:store($action)
		case element(store-files) return
            t:store-files($action)
        case element(remove-collection) return
            xdb:remove($action/@collection)
        case element(remove-document) return
            xdb:remove($action/@collection, $action/@name)
        default return
            ()
};

declare function t:store($action as element(store)) {
    let $type := if ($action/@type) then $action/@type/string() else "application/xml"
    let $data :=
		if ($action/*) then
			$action/*[1]
		else
			$action/string()
	return
        xdb:store($action/@collection, $action/@name, $data, $type)
};

declare function t:store-files($action as element(store-files)) {
    let $type := if ($action/@type) then $action/@type/string() else "application/xml"
    return
        xdb:store-files-from-pattern($action/@collection, $action/@dir, $action/@pattern, $type)
};

declare function t:setup($setup as element(setup)) {
    for $action in $setup/*
    return
        t:setup-action($action)
};

declare function t:tearDown($tearDown as element(tearDown)?) {
    for $action in $tearDown/*
    return
        t:setup-action($action)
};

declare function t:declare-variable($var as element(variable)) as item()? {
    let $children := $var/*
    return
        if (empty($children)) then
            string-join($var/node(), '')
        else
            util:serialize($children, ())
};

declare function t:init-prolog($test as element(test)) {
	let $imports := $test/../imports
	let $vars :=
	    string-join(
        	for $var in $test/../variable
        	return
        	    concat("declare variable $", $var/@name, " := ", t:declare-variable($var), ";"),
            ""
        )
	return
		string-join(($imports, $vars, $test/../functions), '')
};

declare function t:test($result as item()*) {
    if ($result instance of xs:boolean) then
        $result
    else
        exists($result)
};

declare function t:run-test($test as element(test), $count as xs:integer) {
	let $context := t:init-prolog($test)
	let $null :=
	   if ($test/@trace eq 'yes') then
	       (system:clear-trace(), system:enable-tracing(true(), false()))
       else ()
    let $highlight-option := concat("highlight-matches=",
          if ($test/expected//@*[matches(., '^(\|{3}).*\1$')] and $test/expected//exist:match) then "both"
          else if ($test/expected//@*[matches(., '^(\|{3}).*\1$')]) then "attributes"
          else if ($test/expected//exist:match) then "elements"
          else "none"
          )
    let $serialize-options :=
      let $decls := ($test/../*[name() ne 'test']|$test/code)[matches(., 'declare[\- ]option(\(("|')|\s+)exist:serialize(\2,)?\s+("|').*?\4[;)]')]
      let $ops1 := $decls/replace(., "declare[\- ]option(\(("|')|\s+)exist:serialize(\2,)?\s+("|')(.*?)\4[;)]", "_|$5_")
      let $ops2 :=
        for $a in $ops1
        for $b in tokenize($a, '_')[starts-with(., '|')]
        return tokenize(substring-after($b, '|'), '\s+')
      return if (count($ops2[matches(., 'highlight-matches')]))
        then string-join($ops2, ' ')
        else string-join(($ops2, $highlight-option), ' ')
    let $queryOutput :=
		util:catch("*",
			util:eval(concat($context, $test/code/string())),
			<error>Compilation error: {$util:exception-message}</error>
		)
	let $output := if ($test/@trace eq 'yes') then system:trace() else $queryOutput
    let $expanded :=
		if ($output instance of node()) then
        	util:expand($output, $serialize-options)
		else
			$output
    let $expected :=
        if ($test/@output eq 'text') then
            data($test/expected)
        else $test/expected/node()
    let $OK :=
        if ($test/xpath) then
            t:test(t:xpath($output, $test/xpath))
        else if ($test/@output eq 'text') then
            normalize-space(string-join(for $x in $output return string($x),' ')) eq normalize-space($expected)
        else
            let $xn := t:normalize($expanded)
            let $en := t:normalize($expected)
            return
        	   deep-equal($xn, $en)
    return
        <test n="{$count}" pass="{$OK}">
        {
            if (not($OK)) then
                ($test/task, $test/expected, <result>{$expanded}</result>)
            else
                ()
        }
        </test>
};

declare function t:normalize($nodes as node()*) {
	for $node in $nodes return t:normalize-node($node)
};

declare function t:normalize-node($node as node()) {
	typeswitch ($node)
		case element() return
			element { node-name($node) } {
				$node/@*, for $child in $node/node() return t:normalize-node($child)
			}
		case text() return
			let $norm := normalize-space($node)
			return
				if (string-length($norm) eq 0) then () else $node
		default return
			$node
};

declare function t:xpath($output as item()*, $xpath as element()) {
    for $prefix in in-scope-prefixes($xpath)
    let $nsuri := namespace-uri-for-prefix($prefix, $xpath)
    return
        if ($prefix != 'xml') then
            util:declare-namespace($prefix, $nsuri)
        else (),
    let $expr := $xpath/string()
    return
        if (matches($expr, "^\s*/")) then
            util:eval(concat("$output", $expr))
        else
            util:eval($expr)
};

declare function t:run-testSet($set as element(TestSet)) {
    let $copy := util:expand($set)
    let $null := t:setup($copy/setup)
    let $result := util:expand(
           <TestSet>
           {$copy/testName}
           {$copy/description}
           {
               for $test at $p in $copy/test
               return
                   t:run-test($test, $p)
           }
           </TestSet>
        )
    let $null := t:tearDown($copy/tearDown)
    return $result
};

declare function t:format-testResult($result) {
    let $formatted :=

{$result/testName}

{$result/description/p}

          {for $test in $result//test
           return

                    {$test/@n/.}
                    {$test/@pass/.}

          }

     return
        $formatted
};