Created
May 19, 2020 11:54
-
-
Save grtjn/d7bd4b65e9b616bf62b0a5196bc006a2 to your computer and use it in GitHub Desktop.
Searching regions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
declare option xdmp:mapping "false"; | |
declare function local:matchRegion0($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
let $in-country := '/country[name-variants/variant = $country]/regions/region/name-variants/variant' | |
let $anywhere := '/country/regions/region/name-variants/variant' | |
let $found := cts:contains(?, $region) | |
return | |
if ($country) | |
then ($found(xdmp:value($in-country))) | |
else ($found(xdmp:value($anywhere))) | |
}; | |
(: | |
declare function local:matchRegion1($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
let $in-country := '/country[name-variants/variant = $country]/regions/region/name-variants/variant' | |
let $anywhere := '/country/regions/region/name-variants/variant' | |
let $found := xdmp:value(?) => cts:contains($region) (: Cannot feed incomplete function as first arg to cts:contains! :) | |
return | |
if ($country) | |
then ($found($in-country)) | |
else ($found($anywhere)) | |
}; | |
:) | |
declare function local:matchRegion2($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
let $in-country := '/country[name-variants/variant = $country]/regions/region/name-variants/variant' | |
let $anywhere := '/country/regions/region/name-variants/variant' | |
let $found := xdmp:value(?) ! cts:contains(?,$region) (: No . behind !, so first is replaced by second :) | |
return | |
if ($country) | |
then ($found($in-country)) | |
else ($found($anywhere)) | |
}; | |
declare function local:matchRegion3($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
(: value of $country gets lost somewhere, might be a scoping issue | |
let $in-country := '/country[name-variants/variant = $country]/regions/region/name-variants/variant' | |
:) | |
(: you can insert its value directly instead :) | |
let $in-country := '/country[name-variants/variant = "' || $country || '"]/regions/region/name-variants/variant' | |
let $anywhere := '/country/regions/region/name-variants/variant' | |
let $a := function($x,$y){cts:contains(xdmp:value($x),$y)} | |
let $found := $a(?,$region) | |
return | |
if ($country) | |
then ($found($in-country)) | |
else ($found($anywhere)) | |
}; | |
declare function local:matchRegion3b($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
let $in-country := 'collection("regions")/country[name-variants/variant = $country]/regions' | |
let $anywhere := 'collection("regions")/country' | |
(: It also works if you name the function arguments properly :) | |
let $found := function($country, $path) { | |
cts:contains(xdmp:value($path), $region) | |
} | |
return | |
if ($country) | |
then ($found($country, $in-country)) | |
else ($found($country, $anywhere)) | |
}; | |
declare function local:matchRegionGJ1($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
exists( | |
if (exists($country)) then | |
collection('regions')/country[name = $country or name-variants/variant = $country]/regions/region[name = $region or name-variants/variant = $region] | |
else | |
collection('regions')/country/regions/region[name = $region or name-variants/variant = $region] | |
) | |
}; | |
declare function local:matchRegionGJ2($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
if (exists($country)) then | |
cts:contains(collection('regions')/country[name = $country or name-variants/variant = $country], $region) | |
else | |
cts:contains(collection('regions'), $region) | |
}; | |
declare function local:matchRegionGJ3($region as xs:string, $country as xs:string?) | |
as xs:boolean | |
{ | |
let $country-query := | |
if (exists($country)) then | |
cts:element-query(xs:QName('country'), cts:element-value-query(xs:QName('variant'), $country)) | |
else () | |
return xdmp:exists( | |
cts:search(collection('regions'), cts:and-query(( | |
$country-query, | |
cts:element-query(xs:QName('region'), cts:element-value-query(xs:QName('variant'), $region)) | |
))) | |
) | |
}; | |
local:matchRegion0("ohio","usa"), | |
local:matchRegion2("ohio","usa"), | |
local:matchRegion3("ohio","usa"), | |
local:matchRegion3b("ohio","usa"), | |
local:matchRegionGJ1("ohio","usa"), | |
local:matchRegionGJ2("ohio","usa"), | |
local:matchRegionGJ3("ohio","usa") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment