[FIX] DBA: Use 'basex' instead of 'adaptive' serialization

(:~
 : Web functions.
 :
 : @author Christian Grün, BaseX Team, 2014-16
 :)
module namespace util = 'dba/util';

import module namespace Request = 'http://exquery.org/ns/request';
import module namespace cons = 'dba/cons' at 'cons.xqm';

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @return result of query
 :)
declare function util:query(
  $query  as xs:string?
) as xs:string {
  util:query($query, '', map {})
};

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @param  $map    bindings
 : @param  $vars   variable bindings
 : @return result of query
 :)
declare function util:query(
  $query  as xs:string?,
  $map    as xs:string,
  $vars   as map(*)
) as xs:string {
  let $limit := $cons:OPTION($cons:K-MAX-CHARS)
  let $query := if($query) then $query else '()'
  let $q := "xquery:eval($query, map {" || $map || "}, " || util:query-options() || ")"
  let $s := "serialize(" || $q || ", map{ 'limit': $limit*2, 'method': 'basex' })"
  return util:eval(
    $s ||
    "! (if(string-length(.) > $limit) then substring(., 1, $limit) || '...' else .)",
    map:merge((map { 'query': $query, 'limit': $limit }, $vars))
  )
};

(:~
 : Runs an updating query.
 : @param  $query  query
 : @return empty sequence
 :)
declare %updating function util:update-query(
  $query  as xs:string?
) {
  let $q := "xquery:update($query, map { }, " || util:query-options() || ")"
  return util:update($q, map { 'query': if($query) then $query else '()' })
};

(:~
 : Returns the options for evaluating a query.
 : @return options
 :)
declare %private function util:query-options() {
  "map { 'timeout':" || $cons:OPTION($cons:K-TIMEOUT) ||
       ",'memory':" || $cons:OPTION($cons:K-MEMORY) ||
       ",'permission':'" || $cons:OPTION($cons:K-PERMISSION) || "' }"
};

(:~
 : Evaluates the specified query locally or on a remote server and returns the results.
 : @param  $query  query to be executed
 : @return result
 :)
declare function util:eval(
  $query  as xs:string
) as item()* {
  util:eval($query, map {})
};

(:~
 : Evaluates the specified query locally or remotely and returns the resulting items.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare function util:eval(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($cons:SESSION/host) then (
    util:remote-query($query, $vars)
  ) else (
    prof:dump($query),
    xquery:eval($query, $vars)
  )
};

(:~
 : Evaluates the specified updating query locally or remotely.
 : @param  $query  query to be executed
 : @param  $vars   variables
 :)
declare %updating function util:update(
  $query  as xs:string,
  $vars   as map(*)
) {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($cons:SESSION/host) then (
    prof:void(util:remote-query($query, $vars))
  ) else (
    xquery:update($query, $vars)
  )
};

(:~
 : Evaluates the specified query on a remote server and returns the results.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare %private function util:remote-query(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $id := client:connect(
    $cons:SESSION/host, $cons:SESSION/port,
    $cons:SESSION/name, $cons:SESSION/pass
  )
  return (
    client:query($id, $query, $vars),
    client:close($id)
  )
};

(:~
 : Web functions.
 :
 : @author Christian Grün, BaseX Team, 2014-16
 :)
module namespace util = 'dba/util';

import module namespace Request = 'http://exquery.org/ns/request';
import module namespace cons = 'dba/cons' at 'cons.xqm';

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @return result of query
 :)
declare function util:query(
  $query  as xs:string?
) as xs:string {
  util:query($query, '', map {})
};

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @param  $map    bindings
 : @param  $vars   variable bindings
 : @return result of query
 :)
declare function util:query(
  $query  as xs:string?,
  $map    as xs:string,
  $vars   as map(*)
) as xs:string {
  let $limit := $cons:OPTION($cons:K-MAX-CHARS)
  let $query := if($query) then $query else '()'
  let $q := "xquery:eval($query, map {" || $map || "}, " || util:query-options() || ")"
  let $s := "serialize(" || $q || ", map{ 'limit': $limit*2, 'method': 'adaptive' })"
  return util:eval(
    $s ||
    "! (if(string-length(.) > $limit) then substring(., 1, $limit) || '...' else .)",
    map:merge((map { 'query': $query, 'limit': $limit }, $vars))
  )
};

(:~
 : Runs an updating query.
 : @param  $query  query
 : @return empty sequence
 :)
declare %updating function util:update-query(
  $query  as xs:string?
) {
  let $q := "xquery:update($query, map { }, " || util:query-options() || ")"
  return util:update($q, map { 'query': if($query) then $query else '()' })
};

(:~
 : Returns the options for evaluating a query.
 : @return options
 :)
declare %private function util:query-options() {
  "map { 'timeout':" || $cons:OPTION($cons:K-TIMEOUT) ||
       ",'memory':" || $cons:OPTION($cons:K-MEMORY) ||
       ",'permission':'" || $cons:OPTION($cons:K-PERMISSION) || "' }"
};

(:~
 : Evaluates the specified query locally or on a remote server and returns the results.
 : @param  $query  query to be executed
 : @return result
 :)
declare function util:eval(
  $query  as xs:string
) as item()* {
  util:eval($query, map {})
};

(:~
 : Evaluates the specified query locally or remotely and returns the resulting items.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare function util:eval(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($cons:SESSION/host) then (
    util:remote-query($query, $vars)
  ) else (
    xquery:eval($query, $vars)
  )
};

(:~
 : Evaluates the specified updating query locally or remotely.
 : @param  $query  query to be executed
 : @param  $vars   variables
 :)
declare %updating function util:update(
  $query  as xs:string,
  $vars   as map(*)
) {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($cons:SESSION/host) then (
    prof:void(util:remote-query($query, $vars))
  ) else (
    xquery:update($query, $vars)
  )
};

(:~
 : Evaluates the specified query on a remote server and returns the results.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare %private function util:remote-query(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $id := client:connect(
    $cons:SESSION/host, $cons:SESSION/port,
    $cons:SESSION/name, $cons:SESSION/pass
  )
  return (
    client:query($id, $query, $vars),
    client:close($id)
  )
};


[bugfix] Correct test for bugfixed Xerces 2.12.1

xquery version "3.1";

module namespace val ="http://exist-db.org/xquery/test/validation";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare variable $val:XML := <root>
                                <value1>20</value1>
                                <value2>30</value2>
                            </root>;

declare variable $val:XSD11_1 := <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
                                        <xs:element name="root">
                                          <xs:complexType>
                                            <xs:sequence>
                                              <xs:element ref="value1"/>
                                              <xs:element ref="value2"/>
                                            </xs:sequence>
                                            <!-- XSD11 assertion -->
                                           <xs:assert test="value2 gt value1"></xs:assert>
                                          </xs:complexType>
                                        </xs:element>
                                        <xs:element name="value1" type="xs:integer"/>
                                        <xs:element name="value2" type="xs:integer"/>
                                      </xs:schema>;

declare variable $val:XSD11_2 := <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
                                        <xs:element name="root">
                                          <xs:complexType>
                                            <xs:sequence>
                                              <xs:element ref="value1"/>
                                              <xs:element ref="value2"/>
                                            </xs:sequence>
                                            <!-- XSD11 assertion -->
                                           <xs:assert test="value2 lt value1"></xs:assert>
                                          </xs:complexType>
                                        </xs:element>
                                        <xs:element name="value1" type="xs:integer"/>
                                        <xs:element name="value2" type="xs:integer"/>
                                      </xs:schema>;

(: Verify that for JAXV it is required to specify the XSD version :)
declare
    %test:assertEquals("s4s-elt-invalid-content.1: The content of '#AnonType_root' is invalid. Element 'assert' is invalid, misplaced, or occurs too often.")
function val:xsd11_no_xsd11_namespace() {
    data(validation:jaxv-report($val:XML ,$val:XSD11_1)//message)
};

(: Good weather scenario : XML is valid:)
declare
    %test:assertEquals("valid")
function val:xsd11_valid() {
    data(validation:jaxv-report($val:XML, $val:XSD11_1, "http://www.w3.org/XML/XMLSchema/v1.1")//status)
};

(: Good weather scenario : XML is invalid:)
declare
    %test:assertEquals("cvc-assertion: Assertion evaluation ('value2 lt value1') for element 'root' on schema type '#AnonType_root' did not succeed. ")
function val:xsd11_invalid() {
    data(validation:jaxv-report($val:XML, $val:XSD11_2, "http://www.w3.org/XML/XMLSchema/v1.1")//message)
};


Annotate the EXPath geo functions with versioned type restrictions.

xquery version "3.0";
(:~
 : Geo Module (EXPath Candidate Module 30 September 2010)
 :
 : @see http://expath.org/spec/geo
 : @see http://docs.basex.org/wiki/Geo_Module
 :)
module namespace geo = "http://expath.org/ns/geo";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare namespace gml = "http://www.opengis.net/gml";

declare option o:requires "expath-geo/1.0-20100930";

declare option o:implements-module "basex/7.6 as expath-geo/1.0-20100930";
declare option o:requires-import "basex/7.6; location-uri=(none)";

declare %a:restrict-since("return", "basex", "7.6", "xs:integer")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:dimension($geometry as node()) as xs:integer? external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:coordinate-dimension($geometry as node()) as xs:integer external;
declare %a:restrict-since("return", "basex", "7.6", "xs:QName")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:geometry-type($geometry as node()) as xs:QName? external;
declare %a:restrict-since("return", "expath-geo", "1.0-20100930", "xs:anyURI?")
        %a:restrict-since("return", "basex", "7.6", "xs:integer")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:srid($geometry as node()) as (xs:anyURI?|xs:integer) external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:envelope($geometry as node()) as element(gml:Envelope) external;
declare %a:restrict-since("return", "basex", "7.6", "xs:string")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:as-text($geometry as node()) as xs:string? external;
declare %a:restrict-since("return", "basex", "7.6", "xs:base64Binary")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:as-binary($geometry as node()) as xs:base64Binary? external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:is-empty($geometry as node()) as xs:boolean external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:is-simple($geometry as node()) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:is-3d($geometry as node()) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:is-measured($geometry as node()) as xs:boolean external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:boundary($geometry as node()) as element()? external;

declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:equals($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:disjoint($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:intersects($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:touches($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:crosses($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:within($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:contains($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:overlaps($geometry1 as node(), $geometry2 as node()) as xs:boolean external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:relate($geometry1 as node(), $geometry2 as node(), $intersectionMatrix as xs:string) as xs:boolean external;

declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:distance($geometry1 as node(), $geometry2 as node()) as xs:double external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:buffer($geometry as node(), $distance as xs:double) as element() external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:convex-hull($geometry as node()) as element() external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:intersection($geometry1 as node(), $geometry2 as node()) as element()? external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:union($geometry1 as node(), $geometry2 as node()) as element() external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:difference($geometry1 as node(), $geometry2 as node()) as element()? external;
declare %a:restrict-since("$geometry1", "basex", "7.6", "element()")
        %a:restrict-since("$geometry2", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:sym-difference($geometry1 as node(), $geometry2 as node()) as element()? external;

declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:num-geometries($geometry as node()) as xs:integer external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:geometry-n($geometry as node(), $geoNumber as xs:integer) as element() external;

declare %a:restrict-since("$point", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:x($point as node()) as xs:double external;
declare %a:restrict-since("return", "expath-geo", "1.0-20100930", "xs:double")
        %a:restrict-since("$point", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:y($point as node()) as xs:double? external;
declare %a:restrict-since("$point", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:z($point as node()) as xs:double? external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:m($point as node()) as xs:double? external;

declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:length($geometry as node()) as xs:double external;
declare %a:restrict-since("$line", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:start-point($line as node()) as element(gml:Point) external;
declare %a:restrict-since("$line", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:end-point($line as node()) as element(gml:Point) external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:is-closed($geometry as node()) as xs:boolean external;
declare %a:restrict-since("$line", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:is-ring($line as node()) as xs:boolean external;
declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:num-points($geometry as node()) as xs:integer external;
declare %a:restrict-since("return", "basex", "7.6", "element()")
        %a:restrict-since("$line", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:point-n($line as node(), $n as xs:integer) as node() external;

declare %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:area($geometry as node()) as xs:double external;
declare %a:restrict-since("return", "basex", "7.6", "element()")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:centroid($geometry as node()) as node() external;
declare %a:restrict-since("return", "basex", "7.6", "element()")
        %a:restrict-since("$geometry", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:point-on-surface($geometry as node()) as node() external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:num-patches($surface as node()) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:patch-n($surface as node(), $n as xs:integer) as node() external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:bounding-polgons($surface as node(), $polygon as node()) as element(gml:Polygon)* external;

declare %a:restrict-since("$polygon", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:exterior-ring($polygon as node()) as element(gml:LineString) external;
declare %a:restrict-since("$polygon", "basex", "7.6", "element()")
        %a:since("expath-geo", "1.0-20100930") function geo:num-interior-ring($polygon as node()) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") function geo:interior-ring-n($polygon as element(), $n as xs:integer) as element(gml:LineString) external;

xquery version "3.0";
(:~
 : Geo Module (EXPath Candidate Module 30 September 2010)
 :
 : @see http://expath.org/spec/geo
 : @see http://docs.basex.org/wiki/Geo_Module
 :)
module namespace geo = "http://expath.org/ns/geo";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare namespace gml = "http://www.opengis.net/gml";

declare option o:requires "expath-geo/1.0-20100930";

declare option o:implements-module "basex/7.6 as expath-geo/1.0-20100930";
declare option o:requires-import "basex/7.6; location-uri=(none)";

declare %a:since("expath-geo", "1.0-20100930") function geo:dimension($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]xs:integer? [basex/7.6]xs:integer :) external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:coordinate-dimension($geometry as node()) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") function geo:geometry-type($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]xs:QName? [basex/7.6]xs:QName :) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:srid($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]xs:anyURI? [basex/7.6]xs:integer :) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:envelope($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element(gml:Envelope) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:as-text($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]xs:string? [basex/7.6]xs:string :) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:as-binary($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]xs:base64Binary? [basex/7.6]xs:base64Binary :) external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:is-empty($geometry as node()) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:is-simple($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:is-3d($geometry as node()) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:is-measured($geometry as node()) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:boundary($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element()? external;

declare %a:since("expath-geo", "1.0-20100930") function geo:equals($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:disjoint($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:intersects($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:touches($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:crosses($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:within($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:contains($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:overlaps($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:relate($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $intersectionMatrix as xs:string) as xs:boolean external;

declare %a:since("expath-geo", "1.0-20100930") function geo:distance($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:double external;
declare %a:since("expath-geo", "1.0-20100930") function geo:buffer($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $distance as xs:double) as element() external;
declare %a:since("expath-geo", "1.0-20100930") function geo:convex-hull($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element() external;
declare %a:since("expath-geo", "1.0-20100930") function geo:intersection($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element()? external;
declare %a:since("expath-geo", "1.0-20100930") function geo:union($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element() external;
declare %a:since("expath-geo", "1.0-20100930") function geo:difference($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element()? external;
declare %a:since("expath-geo", "1.0-20100930") function geo:sym-difference($geometry1 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geometry2 (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element()? external;

declare %a:since("expath-geo", "1.0-20100930") function geo:num-geometries($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") function geo:geometry-n($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $geoNumber as xs:integer) as element() external;

declare %a:since("expath-geo", "1.0-20100930") function geo:x($point (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:double external;
declare %a:since("expath-geo", "1.0-20100930") function geo:y($point (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]xs:double [basex/7.6]xs:double? :) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:z($point (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:double? external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:m($point as node()) as xs:double? external;

declare %a:since("expath-geo", "1.0-20100930") function geo:length($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:double external;
declare %a:since("expath-geo", "1.0-20100930") function geo:start-point($line (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element(gml:Point) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:end-point($line (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element(gml:Point) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:is-closed($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:is-ring($line (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:boolean external;
declare %a:since("expath-geo", "1.0-20100930") function geo:num-points($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") function geo:point-n($line (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :), $n as xs:integer) (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :) external;

declare %a:since("expath-geo", "1.0-20100930") function geo:area($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:double external;
declare %a:since("expath-geo", "1.0-20100930") function geo:centroid($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:point-on-surface($geometry (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :) external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:num-patches($surface as node()) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:patch-n($surface as node(), $n as xs:integer) as node() external;
declare %a:since("expath-geo", "1.0-20100930") %a:missing("basex", "7.6") function geo:bounding-polgons($surface as node(), $polygon as node()) as element(gml:Polygon)* external;

declare %a:since("expath-geo", "1.0-20100930") function geo:exterior-ring($polygon (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as element(gml:LineString) external;
declare %a:since("expath-geo", "1.0-20100930") function geo:num-interior-ring($polygon (: as [expath-geo/1.0-20100930]node() [basex/7.6]element() :)) as xs:integer external;
declare %a:since("expath-geo", "1.0-20100930") function geo:interior-ring-n($polygon as element(), $n as xs:integer) as element(gml:LineString) external;


Update module path in debug lib

Update module path in debug lib

(:
  Copyright 2012-2018 MarkLogic Corporation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
:)
xquery version "1.0-ml";

module namespace debug = "http://marklogic.com/data-hub/debug";

import module namespace hul = "http://marklogic.com/data-hub/hub-utils-lib"
  at "/data-hub/4/impl/hub-utils-lib.xqy";

declare option xdmp:mapping "false";

declare function debug:enable($enabled as xs:boolean)
{
  xdmp:eval('
    declare namespace debug = "http://marklogic.com/data-hub/debug";
    declare variable $enabled external;

    xdmp:document-insert(
      "/com.marklogic.hub/settings/__debug_enabled__.xml",
      element debug:is-debugging-enabled { if ($enabled) then 1 else 0 },
      xdmp:default-permissions(),
      "hub-core-module")
    ',
    map:new((map:entry("enabled", $enabled))),
    map:new((map:entry("database", xdmp:modules-database()), map:entry("ignoreAmps", fn:true())))
  ),
  hul:invalidate-field-cache("debugging-enabled")
};

(:~
 : Determines whether debugging is on or not
 :
 : @return - boolean: on or off
 :)
declare function debug:on() as xs:boolean
{
  let $key := "debugging-enabled"
  let $flag :=  hul:from-field-cache-or-empty($key, ())
  return
    if (exists($flag)) then
      $flag
    else
      hul:set-field-cache(
        $key,
        xdmp:eval('
          declare namespace debug = "http://marklogic.com/data-hub/debug";
          fn:exists(
            cts:search(
              fn:doc("/com.marklogic.hub/settings/__debug_enabled__.xml"),
              cts:element-value-query(xs:QName("debug:is-debugging-enabled"), "1", ("exact")),
              ("unfiltered", "score-zero", "unchecked", "unfaceted")
            )
          )
        ',(), map:new(map:entry("database", xdmp:modules-database()))),
        ()
      )
};

(:~
 : Logs if debugging is on
 :
 : @param $items - the stuff to log
 :)
declare function debug:log($items)
{
  if (debug:on()) then
    xdmp:log($items)
  else
    ()
};

(:~
 : Dumps the request environment. Useful for debugging
 :)
declare function debug:dump-env()
{
  debug:dump-env(())
};

declare function debug:dump-env($name as xs:string?)
{
  if (debug:on()) then
    let $request-path := xdmp:get-request-path()
    let $request-path :=
      if ($request-path = '/data-hub/4/rest-api/endpoints/resource-service-query.xqy') then
        let $params := fn:string-join(
          for $f in xdmp:get-request-field-names()[fn:starts-with(., "rs:")]
          let $value := xdmp:get-request-field($f)
          return
            $f || "=" || fn:string-join($value, ", "),
        "&")
        return
          "/v1/resources/" || xdmp:get-request-field("name") || "?" || $params
      else
        $request-path
    return
      debug:log((
        "",
        "",
        "################################################################",
        "REQUEST DETAILS:",
        "",
        if ($name) then
          (
            "  **" || $name || "**",
            ""
          )
        else (),
        "  [" || xdmp:get-request-method() || "]  " || $request-path,
        "",
        "  [Headers]",
        for $h in xdmp:get-request-header-names()
        return
          "    " || $h || " => " || xdmp:get-request-header($h),
        "",
        "  [Request Params]",
        for $p in xdmp:get-request-field-names()[fn:not(fn:starts-with(., "rs:"))]
        return
          "    " || $p || " => " || fn:string-join(xdmp:get-request-field($p), ", "),
        let $body :=
          try {
            xdmp:get-request-body()
          }
          catch($ex) {()}
        return
          if (fn:exists($body)) then
          (
            "",
            "  [Body]",
            "  " || xdmp:describe($body, (), ())
          )
          else (),
        "",
        "################################################################",
        "",
        "",
        ""
      ))
  else ()
};

declare function debug:dump-map($m as map:map)
{
  debug:dump-map($m, ())
};

declare function debug:dump-map($m as map:map, $prefix)
{
  for $key in map:keys($m)
  return
    $prefix || $key || " => " || map:get($m, $key)
};

(:
  Copyright 2012-2018 MarkLogic Corporation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
:)
xquery version "1.0-ml";

module namespace debug = "http://marklogic.com/data-hub/debug";

import module namespace hul = "http://marklogic.com/data-hub/hub-utils-lib"
  at "/data-hub/4/impl/hub-utils-lib.xqy";

declare option xdmp:mapping "false";

declare function debug:enable($enabled as xs:boolean)
{
  xdmp:eval('
    declare namespace debug = "http://marklogic.com/data-hub/debug";
    declare variable $enabled external;

    xdmp:document-insert(
      "/com.marklogic.hub/settings/__debug_enabled__.xml",
      element debug:is-debugging-enabled { if ($enabled) then 1 else 0 },
      xdmp:default-permissions(),
      "hub-core-module")
    ',
    map:new((map:entry("enabled", $enabled))),
    map:new((map:entry("database", xdmp:modules-database()), map:entry("ignoreAmps", fn:true())))
  ),
  hul:invalidate-field-cache("debugging-enabled")
};

(:~
 : Determines whether debugging is on or not
 :
 : @return - boolean: on or off
 :)
declare function debug:on() as xs:boolean
{
  let $key := "debugging-enabled"
  let $flag :=  hul:from-field-cache-or-empty($key, ())
  return
    if (exists($flag)) then
      $flag
    else
      hul:set-field-cache(
        $key,
        xdmp:eval('
          declare namespace debug = "http://marklogic.com/data-hub/debug";
          fn:exists(
            cts:search(
              fn:doc("/com.marklogic.hub/settings/__debug_enabled__.xml"),
              cts:element-value-query(xs:QName("debug:is-debugging-enabled"), "1", ("exact")),
              ("unfiltered", "score-zero", "unchecked", "unfaceted")
            )
          )
        ',(), map:new(map:entry("database", xdmp:modules-database()))),
        ()
      )
};

(:~
 : Logs if debugging is on
 :
 : @param $items - the stuff to log
 :)
declare function debug:log($items)
{
  if (debug:on()) then
    xdmp:log($items)
  else
    ()
};

(:~
 : Dumps the request environment. Useful for debugging
 :)
declare function debug:dump-env()
{
  debug:dump-env(())
};

declare function debug:dump-env($name as xs:string?)
{
  if (debug:on()) then
    let $request-path := xdmp:get-request-path()
    let $request-path :=
      if ($request-path = '/MarkLogic/rest-api/endpoints/resource-service-query.xqy') then
        let $params := fn:string-join(
          for $f in xdmp:get-request-field-names()[fn:starts-with(., "rs:")]
          let $value := xdmp:get-request-field($f)
          return
            $f || "=" || fn:string-join($value, ", "),
        "&")
        return
          "/v1/resources/" || xdmp:get-request-field("name") || "?" || $params
      else
        $request-path
    return
      debug:log((
        "",
        "",
        "################################################################",
        "REQUEST DETAILS:",
        "",
        if ($name) then
          (
            "  **" || $name || "**",
            ""
          )
        else (),
        "  [" || xdmp:get-request-method() || "]  " || $request-path,
        "",
        "  [Headers]",
        for $h in xdmp:get-request-header-names()
        return
          "    " || $h || " => " || xdmp:get-request-header($h),
        "",
        "  [Request Params]",
        for $p in xdmp:get-request-field-names()[fn:not(fn:starts-with(., "rs:"))]
        return
          "    " || $p || " => " || fn:string-join(xdmp:get-request-field($p), ", "),
        let $body :=
          try {
            xdmp:get-request-body()
          }
          catch($ex) {()}
        return
          if (fn:exists($body)) then
          (
            "",
            "  [Body]",
            "  " || xdmp:describe($body, (), ())
          )
          else (),
        "",
        "################################################################",
        "",
        "",
        ""
      ))
  else ()
};

declare function debug:dump-map($m as map:map)
{
  debug:dump-map($m, ())
};

declare function debug:dump-map($m as map:map, $prefix)
{
  for $key in map:keys($m)
  return
    $prefix || $key || " => " || map:get($m, $key)
};


s/Zorba/this module/

s/Zorba/this module/

(:
 : Copyright 2006-2009 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
 :)

xquery version "3.0";

 (:~
  : Using this module, you can parse JSON data into XML, manipulate it like any
  : other XML data using XQuery, and serialize the result back as JSON.

  :
  : There are many ways to represent JSON data in XML, some loss-less ("round
  : tripable") and some lossy ("one way").  Loss-less representations preserve
  : the JSON data types boolean, number, and null; lossy
  : representations convert all data to strings.
  :

  : For a loss-less representation, this module implements that proposed by
  : John Snelson.
  : For example:
  :

  :   {
  :     "firstName" : "John",
  :     "lastName" : "Smith",
  :     "address" : {
  :       "streetAddress" : "21 2nd Street",
  :       "city" : "New York",
  :       "state" : "NY",
  :       "postalCode" : 10021
  :     },
  :     "phoneNumbers" : [ "212 732-1234", "646 123-4567" ]
  :   }
  :

  : would be represented as:
  :

  :   <json type="object">
  :     <pair name="firstName" type="string">John</pair>
  :     <pair name="lastName" type="string">Smith</pair>
  :     <pair name="address" type="object">
  :       <pair name="streetAddress" type="string">21 2nd Street</pair>
  :       <pair name="city" type="string">New York</pair>
  :       <pair name="state" type="string">NY</pair>
  :       <pair name="postalCode" type="number">10021</pair>
  :     </pair>
  :     <pair name="phoneNumbers" type="array">
  :       <item type="string">212 732-1234</item>
  :       <item type="string">646 123-4567</item>
  :     </pair>
  :   </json>
  :

  : For a lossy representation, this module implements
  : JsonML (the array form).
  : For example:
  :

  :   [ "person",
  :     { "created" : "2006-11-11T19:23",
  :       "modified" : "2006-12-31T23:59" },
  :     [ "firstName", "Robert" ],
  :     [ "lastName", "Smith" ],
  :     [ "address",
  :       { "type" : "home" },
  :       [ "street", "12345 Sixth Ave" ],
  :       [ "city", "Anytown" ],
  :       [ "state", "CA" ],
  :       [ "postalCode", "98765-4321" ]
  :     ]
  :   ]
  :

  : would be represented as:
  :

  :   <person created="2006-11-11T19:23" modified="2006-12-31T23:59">
  :     <firstName>Robert</firstName>
  :     <lastName>Smith</lastName>
  :     <address type="home">
  :       <street>12345 Sixth Ave</street>
  :       <city>Anytown</city>
  :       <state>CA</state>
  :       <postalCode>98765-4321</postalCode>
  :     </address>
  :   </person>
  :

  :
  : @author Paul J. Lucas
  : @project Zorba/Data Converters/JSON
  :)
module namespace jx = "http://zorba.io/modules/json-xml";

declare namespace an = "http://www.zorba-xquery.com/annotations";
declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://zorba.io/modules/zorba-errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "1.0";

(:~
 : Parses JSON data from a string and returns an XDM instance using one of the
 : representations described above.

 :
 : @param $json The JSON data to parse.
 : @param $options The parsing options, for example:
 :

 : { "json-format" : "JsonML-array" }
 :

 : @return said XDM instance.
 : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
 : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
 : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
 : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
 : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
 : @error zerr:ZJPE0006 if $json is not a valid JSON string.
 : @error zerr:ZJPE0007 if $json contains an unterminated string.
 : @error zerr:ZJPE0008 if $json contains an illegal QName.
 : @example test/rbkt/Queries/zorba/json/json-jsonml_array-parse-01.xq
 :)
declare function jx:json-to-xml(
  $json as xs:string?,
  $options as object()
) as element(*,xs:untyped)*
{
  jx:json-to-xml-internal( $json, $options )
};

(:~
 : Parses JSON data from a string and returns an XDM instance using the Snelson
 : representation described above.

 :
 : @param $json The JSON data to parse.
 : @return said XDM instance.
 : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
 : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
 : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
 : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
 : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
 : @error zerr:ZJPE0006 if $json is not a valid JSON string.
 : @error zerr:ZJPE0007 if $json contains an unterminated string.
 : @error zerr:ZJPE0008 if $json contains an illegal QName.
 : @example test/rbkt/Queries/zorba/json/json-snelson-parse-array-01.xq
 :)
declare function jx:json-to-xml(
  $json as xs:string?
) as element(*,xs:untyped)*
{
  jx:json-to-xml-internal(
    $json, { "json-format" : "Snelson" }
  )
};

(:~
 : Serializes an XDM into JSON using one of the representations described
 : above.

 :
 : @param $xml The XDM to serialize.
 : @param $options The serializing options, for example:
 :

 : { "json-format" : "JsonML-array", "whitespace" : "indent" }
 :

 : @return a JSON string.
 : @error zerr:ZJSE0001 if $xml is not a document or element node.
 : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
 : attribute.
 : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
 : @error zerr:ZJSE0004 if $xml contains an illegal element.
 : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
 : type.
 : @error zerr:ZJSE0006 if $xml contains an illegal child element.
 : @error zerr:ZJSE0007 if $xml contains an illegal text node.
 : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
 : @example test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-01.xq
 :)
declare function jx:xml-to-json(
  $xml as item()*,
  $options as object()
) as xs:string
{
  jx:xml-to-json-internal( $xml, $options )
};

(:~
 : Serializes an XDM into JSON using one of the representations described
 : above.

 :
 : @param $xml The XDM to serialize.
 : @return a JSON string.
 : @error zerr:ZJSE0001 if $xml is not a document or element node.
 : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
 : attribute.
 : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
 : @error zerr:ZJSE0004 if $xml contains an illegal element.
 : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
 : type.
 : @error zerr:ZJSE0006 if $xml contains an illegal child element.
 : @error zerr:ZJSE0007 if $xml contains an illegal text node.
 : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
 : @example test/rbkt/Queries/zorba/json/json-snelson-serialize-array-01.xq
 :)
declare function jx:xml-to-json(
  $xml as item()*
) as xs:string
{
  jx:xml-to-json-internal($xml, { "json-format" : "Snelson" })
};

(:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)

declare %private function jx:json-to-xml-internal(
  $json as xs:string?,
  $options as item()?
) as element()* external;

declare %an:streamable %private function jx:xml-to-json-internal(
  $xml as item()*,
  $options as item()?
) as xs:string external;

(: vim:set et sw=2 ts=2: :)

(:
 : Copyright 2006-2009 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
 :)

xquery version "3.0";

 (:~
  : Using this module, you can parse JSON data into XML, manipulate it like any
  : other XML data using XQuery, and serialize the result back as JSON.

  :
  : There are many ways to represent JSON data in XML, some loss-less ("round
  : tripable") and some lossy ("one way").  Loss-less representations preserve
  : the JSON data types boolean, number, and null; lossy
  : representations convert all data to strings.
  :

  : For a loss-less representation, Zorba implements that proposed by
  : John Snelson.
  : For example:
  :

  :   {
  :     "firstName" : "John",
  :     "lastName" : "Smith",
  :     "address" : {
  :       "streetAddress" : "21 2nd Street",
  :       "city" : "New York",
  :       "state" : "NY",
  :       "postalCode" : 10021
  :     },
  :     "phoneNumbers" : [ "212 732-1234", "646 123-4567" ]
  :   }
  :

  : would be represented as:
  :

  :   <json type="object">
  :     <pair name="firstName" type="string">John</pair>
  :     <pair name="lastName" type="string">Smith</pair>
  :     <pair name="address" type="object">
  :       <pair name="streetAddress" type="string">21 2nd Street</pair>
  :       <pair name="city" type="string">New York</pair>
  :       <pair name="state" type="string">NY</pair>
  :       <pair name="postalCode" type="number">10021</pair>
  :     </pair>
  :     <pair name="phoneNumbers" type="array">
  :       <item type="string">212 732-1234</item>
  :       <item type="string">646 123-4567</item>
  :     </pair>
  :   </json>
  :

  : For a lossy representation, Zorba implements
  : JsonML (the array form).
  : For example:
  :

  :   [ "person",
  :     { "created" : "2006-11-11T19:23",
  :       "modified" : "2006-12-31T23:59" },
  :     [ "firstName", "Robert" ],
  :     [ "lastName", "Smith" ],
  :     [ "address",
  :       { "type" : "home" },
  :       [ "street", "12345 Sixth Ave" ],
  :       [ "city", "Anytown" ],
  :       [ "state", "CA" ],
  :       [ "postalCode", "98765-4321" ]
  :     ]
  :   ]
  :

  : would be represented as:
  :

  :   <person created="2006-11-11T19:23" modified="2006-12-31T23:59">
  :     <firstName>Robert</firstName>
  :     <lastName>Smith</lastName>
  :     <address type="home">
  :       <street>12345 Sixth Ave</street>
  :       <city>Anytown</city>
  :       <state>CA</state>
  :       <postalCode>98765-4321</postalCode>
  :     </address>
  :   </person>
  :

  :
  : @author Paul J. Lucas
  : @project Zorba/Data Converters/JSON
  :)
module namespace jx = "http://zorba.io/modules/json-xml";

declare namespace an = "http://www.zorba-xquery.com/annotations";
declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://zorba.io/modules/zorba-errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "1.0";

(:~
 : Parses JSON data from a string and returns an XDM instance using one of the
 : representations described above.

 :
 : @param $json The JSON data to parse.
 : @param $options The parsing options, for example:
 :

 : { "json-format" : "JsonML-array" }
 :

 : @return said XDM instance.
 : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
 : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
 : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
 : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
 : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
 : @error zerr:ZJPE0006 if $json is not a valid JSON string.
 : @error zerr:ZJPE0007 if $json contains an unterminated string.
 : @error zerr:ZJPE0008 if $json contains an illegal QName.
 : @example test/rbkt/Queries/zorba/json/json-jsonml_array-parse-01.xq
 :)
declare function jx:json-to-xml(
  $json as xs:string?,
  $options as object()
) as element(*,xs:untyped)*
{
  jx:json-to-xml-internal( $json, $options )
};

(:~
 : Parses JSON data from a string and returns an XDM instance using the Snelson
 : representation described above.

 :
 : @param $json The JSON data to parse.
 : @return said XDM instance.
 : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
 : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
 : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
 : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
 : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
 : @error zerr:ZJPE0006 if $json is not a valid JSON string.
 : @error zerr:ZJPE0007 if $json contains an unterminated string.
 : @error zerr:ZJPE0008 if $json contains an illegal QName.
 : @example test/rbkt/Queries/zorba/json/json-snelson-parse-array-01.xq
 :)
declare function jx:json-to-xml(
  $json as xs:string?
) as element(*,xs:untyped)*
{
  jx:json-to-xml-internal(
    $json, { "json-format" : "Snelson" }
  )
};

(:~
 : Serializes an XDM into JSON using one of the representations described
 : above.

 :
 : @param $xml The XDM to serialize.
 : @param $options The serializing options, for example:
 :

 : { "json-format" : "JsonML-array", "whitespace" : "indent" }
 :

 : @return a JSON string.
 : @error zerr:ZJSE0001 if $xml is not a document or element node.
 : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
 : attribute.
 : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
 : @error zerr:ZJSE0004 if $xml contains an illegal element.
 : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
 : type.
 : @error zerr:ZJSE0006 if $xml contains an illegal child element.
 : @error zerr:ZJSE0007 if $xml contains an illegal text node.
 : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
 : @example test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-01.xq
 :)
declare function jx:xml-to-json(
  $xml as item()*,
  $options as object()
) as xs:string
{
  jx:xml-to-json-internal( $xml, $options )
};

(:~
 : Serializes an XDM into JSON using one of the representations described
 : above.

 :
 : @param $xml The XDM to serialize.
 : @return a JSON string.
 : @error zerr:ZJSE0001 if $xml is not a document or element node.
 : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
 : attribute.
 : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
 : @error zerr:ZJSE0004 if $xml contains an illegal element.
 : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
 : type.
 : @error zerr:ZJSE0006 if $xml contains an illegal child element.
 : @error zerr:ZJSE0007 if $xml contains an illegal text node.
 : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
 : @example test/rbkt/Queries/zorba/json/json-snelson-serialize-array-01.xq
 :)
declare function jx:xml-to-json(
  $xml as item()*
) as xs:string
{
  jx:xml-to-json-internal($xml, { "json-format" : "Snelson" })
};

(:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)

declare %private function jx:json-to-xml-internal(
  $json as xs:string?,
  $options as item()?
) as element()* external;

declare %an:streamable %private function jx:xml-to-json-internal(
  $xml as item()*,
  $options as item()?
) as xs:string external;

(: vim:set et sw=2 ts=2: :)


Annotate the MarkLogic rdf functions with versioned type restrictions.

xquery version "1.0-ml";
(:~
 : MarkLogic rdf functions
 :
 : @see https://docs.marklogic.com/rdf
 :)
module namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare option o:requires "marklogic/7.0";

declare %a:since("marklogic", "7.0") function rdf:langString($string as xs:string, $lang as xs:string) as rdf:langString external;
declare %a:restrict-until("$options", "marklogic", "8.0", "sem:unknown")
        %a:restrict-since("$options", "marklogic", "8.0", "rdf:langString")
        %a:since("marklogic", "7.0") function rdf:langString-language($val as (sem:unknown|rdf:langString)) as xs:string external;

xquery version "1.0-ml";
(:~
 : MarkLogic rdf functions
 :
 : @see https://docs.marklogic.com/rdf
 :)
module namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare option o:requires "marklogic/7.0";

declare %a:since("marklogic", "7.0") function rdf:langString($string as xs:string, $lang as xs:string) as rdf:langString external;
declare %a:since("marklogic", "7.0") function rdf:langString-language($val (: as [7.0]sem:unknown [8.0]rdf:langString :)) as xs:string external;