Skip to content

Instantly share code, notes, and snippets.

@kurtcagle
Last active December 21, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtcagle/3e047942d2b4c2ee3b2e to your computer and use it in GitHub Desktop.
Save kurtcagle/3e047942d2b4c2ee3b2e to your computer and use it in GitHub Desktop.
Semantical ns: module, used for managing namespaces in both XQuery and semantic applications
module namespace ns = "http://semanticalllc.com/ns/namespace#";
import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy";
declare variable $ns:source-path := "/models/namespaces.ttl";
declare function ns:load($src as xs:string?) as item()*{
let $src := if ($src) then $src else $ns:source-path
let $triples := sem:rdf-parse(fn:unparsed-text($src),"turtle")
let $graph-ns := sem:iri("http://semanticalllc.com/ns/graph/namespace#")
return (
sem:graph-delete($graph-ns),
sem:graph-insert($graph-ns,$triples),
xdmp:set-server-field("ns:prefix-map",ns:prefix-map())
)
};
declare function ns:load(){
ns:load($ns:source-path)
};
declare function ns:serialize($format as xs:string,$prefixes as xs:string*) as item()* {
let $prefix-map := xdmp:get-server-field("ns:prefix-map")
return
if ($prefix-map instance of map:map) then
let $prefixes := if (fn:empty($prefixes)) then map:keys($prefix-map) else $prefixes
return
switch($format)
case "turtle" return
fn:string-join(
for $key in $prefixes order by $key return
("@prefix "||$key||": <"||map:get($prefix-map,$key)||">."),
"&#13;")
case "sparql" return
fn:string-join(
for $key in $prefixes order by $key return
("prefix "||$key||": <"||map:get($prefix-map,$key)||">"),
"&#13;")
case "prefix-map" return
$prefix-map
case "namespace-map" return
map:new(for $key in map:keys($prefix-map) return map:entry(map:get($prefix-map,$key),$key))
case "xquery" return
fn:string-join(for $key in $prefixes return
('declare namespace '||$key||' = "'||map:get($prefix-map,$key)||'";'),
"&#13;")
case "rdfa" return
fn:string-join(for $key in $prefixes return
$key ||": "||map:get($prefix-map,$key)," ")
case "xmlns" return
(
for $key in $prefixes return
namespace {$key} {map:get($prefix-map,$key)}
)
case "xmlns-str" return
fn:string-join(
for $key in $prefixes return 'xmlns:'||$key||'="'||map:get($prefix-map,$key)||'"',"&#13;"
)
default return fn:error(xs:QName("ns:ERR_UNKN_SERIAL_FORMAT"),"Unknown serialization format")
else
let $preface := "prefix ns: <http://semanticalllc.com/ns/namespace#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix class: <http://semanticalllc.com/ns/core/class/>
prefix graph: <http://semanticalllc.com/ns/core/graph/>
"
let $query := $preface || "select ?prefix ?namespace from graph:namespace where {
?ns rdf:type class:Namespace.
?ns ns:prefix ?prefix.
?ns ns:namespace ?namespace.
} order by ?prefix"
let $ns-maps := sem:sparql($query,())
return
if ($format = "prefix-map") then
let $prefix-map := xdmp:get-server-field("ns:prefix-map")
return if ($prefix-map instance of map:map) then
$prefix-map else
let $prefix-map :=
map:new(for $ns-map in $ns-maps return
map:entry(map:get($ns-map,"prefix"),map:get($ns-map,"namespace")))
return ($prefix-map,xdmp:set-server-field("ns:prefix-map",$prefix-map))[last()]
else fn:error(xs:QName("ns:ERR_NAMESPACES_NOT_LOADED"),"ns:load() must be called before invoking the '"||$format||"' serialization.")
};
declare function ns:reload(){
xdmp:eval('
import module namespace ns="http://semanticalllc.com/ns/namespace#" at "/lib/ns.xqy";
xdmp:set-server-field("ns:prefix-map",());
import module namespace ns="http://semanticalllc.com/ns/namespace#" at "/lib/ns.xqy";
ns:load()')
};
declare function ns:sparql($prefixes as xs:string*) as xs:string? {
xs:string(ns:serialize("sparql",$prefixes))
};
declare function ns:sparql() as xs:string?{
xs:string(ns:serialize("sparql",()))
};
declare function ns:xquery($prefixes as xs:string*) as xs:string? {
xs:string(ns:serialize("xquery",$prefixes))
};
declare function ns:xquery() as xs:string? {
xs:string(ns:serialize("xquery",()))
};
declare function ns:turtle($prefixes as xs:string) as xs:string? {
xs:string(ns:serialize("turtle",$prefixes))
};
declare function ns:turtle() as xs:string? {
xs:string(ns:serialize("turtle",()))
};
declare function ns:namespace-map() as map:map {
ns:serialize("namespace-map",())
};
declare function ns:prefix-map() as map:map* {
ns:serialize("prefix-map",())
};
declare function ns:rdfa($prefixes as xs:string*) as xs:string {
xs:string(ns:serialize("rdfa",$prefixes))
};
declare function ns:rdfa() as xs:string {
xs:string(ns:serialize("rdfa",()))
};
declare function ns:xmlns($prefixes as xs:string*) as node()* {
ns:serialize("xmlns",$prefixes)
};
declare function ns:xmlns() as node()* {
ns:serialize("xmlns",())
};
declare function ns:xmlns-str($prefixes as xs:string*) as xs:string {
xs:string(ns:serialize("xmlns-str",$prefixes))
};
declare function ns:xmlns-str() as xs:string {
xs:string(ns:serialize("xmlns-str",()))
};
declare function ns:ciri($curie-str as xs:string) as sem:iri?{
sem:curie-expand($curie-str,ns:prefix-map())
};
declare function ns:curie($ciri-str as item()) as xs:string{
sem:curie-shorten(sem:iri($ciri-str),ns:prefix-map())
};
declare function ns:namespace($curie as xs:string) as xs:string* {
map:get(ns:prefix-map(),fn:substring-before($curie,":"))
};
declare function ns:node-to-iri($element-node as node()) as sem:iri {
sem:iri(fn:namespace-uri($element-node)||fn:local-name($element-node))
};
declare function ns:remap-turtle($turtle as xs:string) as xs:string {
let $prefix-lines := for $line in fn:tokenize($turtle,"\n") where fn:starts-with($line,"@prefix ") return $line
let $namespace-map := ns:namespace-map()
let $turtle-map := map:entry("turtle",$turtle)
let $prefix-map := map:new(
for $prefix-line in $prefix-lines return
let $regex := fn:tokenize(fn:replace($prefix-line,"@prefix (.+?): <(.+?)>\s*\.","$1|$2"),"\|")
let $source-prefix := $regex[1]
let $namespace := $regex[2]
return map:entry($regex[1],map:get($namespace-map,$regex[2]))
)
let $_ := for $key in map:keys($prefix-map) return map:put($turtle-map,"turtle",fn:replace(map:get($turtle-map,"turtle"),$key||":",map:get($prefix-map,$key)||":"))
return map:get($turtle-map,"turtle")
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment