forked from LeifW/MusicPath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsparql.xquery
168 lines (138 loc) · 5.22 KB
/
xsparql.xquery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module namespace _xsparql = "http://xsparql.deri.org/XSPARQLer/xsparql.xquery" ;
declare namespace _sparql_result = "http://www.w3.org/2005/sparql-results#";
declare function _xsparql:_rdf_term($NType as xs:string, $V as xs:string, $L as xs:string, $DT as xs:string) as xs:string
{
if($NType = "_sparql_result:literal" or $NType = "literal")
then fn:concat("""",$V,"""", if($L) then fn:concat("@", $L) else "",
if($DT) then fn:concat("^^<", $DT,">") else ""
)
else if ($NType = "_sparql_result:bnode" or $NType = "bnode")
then fn:concat("_:", $V)
else if ($NType = "_sparql_result:uri" or $NType = "uri")
then fn:concat("<", $V, ">")
else ""
};
declare function _xsparql:_empty($rdf_Predicate as xs:string, $rdf_Object as xs:string) as xs:string
{
if(
_xsparql:_validPredicate("", $rdf_Predicate) and
_xsparql:_validObject("", $rdf_Object)
)
then fn:concat($rdf_Predicate, $rdf_Object)
else " "
};
declare function _xsparql:_validBNode($NType as xs:string, $V as xs:string) as xs:boolean
{
if ($NType = "_sparql_result:bnode" or $NType = "bnode")
then fn:true()
else if (fn:matches($V, "^_:[a-z]([a-z|0-9|_])*$", "i"))
then fn:true()
else fn:false()
};
declare function _xsparql:_validUri($NType as xs:string, $V as xs:string) as xs:boolean
{
if ($NType = "_sparql_result:uri" or $NType = "uri")
then fn:true()
else if (
fn:matches($V, "^<[^>]+:[^>]+>$", "i" ) or
fn:matches($V, "^([a-zA-Z]*):(.+)$")
(: fn:matches($V, "^<[^<>""\ {}|\^`]*>$", "i" ) :)
)
then fn:true()
else fn:false()
};
declare function _xsparql:_validLiteral($NType as xs:string, $V as xs:string) as xs:boolean
{
if ($NType = "_sparql_result:literal" or $NType = "literal")
then fn:true()
else if (
fn:matches($V, "^([0-9]+|""[a-zA-Z0-9,:@\n\r\t. ()+<>/]+""([ ]*(@|\^\^)[ ]*[a-zA-Z0-9:.#/<>]+)?)$", "i")
)
then fn:true()
else fn:false()
};
declare function _xsparql:_validSubject($NType as xs:string, $V as xs:string) as xs:boolean
{
if ( _xsparql:_validBNode($NType, $V) or _xsparql:_validUri($NType, $V) )
then fn:true()
else fn:false()
};
declare function _xsparql:_validPredicate($NType as xs:string, $V as xs:string) as xs:boolean
{
if ( _xsparql:_validUri($NType, $V) or fn:matches($V, "^a$") )
then fn:true()
else fn:false()
};
declare function _xsparql:_validObject($NType as xs:string, $V as xs:string) as xs:boolean
{
if (
_xsparql:_validBNode($NType, $V) or
_xsparql:_validUri($NType, $V) or
_xsparql:_validLiteral($NType, $V)
)
then fn:true()
else fn:false()
};
declare function _xsparql:_removeEmpty($result as xs:string) as xs:string
{
let $output :=
fn:replace($result, "^( )*\[( )*\]( )*\.", "", 'm')
let $output :=
fn:replace($output, ";( )*\.", " .", 'm')
return $output
};
declare function _xsparql:_serialize-attributes ( $n as item()* ) as xs:string {
if(fn:empty($n)) then
""
else
fn:concat(" ",
fn:node-name($n[1]), "=""", fn:data($n[1]), """",
_xsparql:_serialize-attributes( fn:subsequence( $n, 2 ) )
)
};
declare function _xsparql:_serialize ( $n as item()* ) as xs:string {
let $r :=
for $item in $n
return typeswitch ($item)
case $e as element()
return fn:concat("<", fn:name($e), _xsparql:_serialize-attributes($e/@*), ">",
_xsparql:_serialize($e/child::node()),
"</", fn:name($e), ">"
)
default
return fn:string($item)
return fn:string-join($r,'')
};
(: declare function _xsparql:_serialize ( $n as item()* ) as xs:string { :)
(: if(fn:empty($n)) then :)
(: "" :)
(: else :)
(: fn:concat( :)
(: typeswitch ($n[1]) :)
(: case $e as element() :)
(: return fn:concat("<", fn:name($e), _xsparql:_serialize-attributes($e/@*), ">", :)
(: _xsparql:_serialize($e/child::node()), :)
(: "</", fn:name($e), ">" :)
(: ) :)
(: default :)
(: return $n[1], :)
(: _xsparql:_serialize( fn:subsequence( $n, 2 ) ) :)
(: ) :)
(: }; :)
declare function _xsparql:_rdf_term($Node as node(), $V as xs:string) as xs:string
{
let $NType := name($Node/*)
let $DT := string($Node/*/@datatype)
let $L := string($Node/*/@lang)
let $rdf_term :=
if($NType = "_sparql_result:literal" or $NType = "literal")
then fn:concat("""",$V,"""", if($L) then fn:concat("@", $L) else "",
if($DT) then fn:concat("^^<", $DT,">") else ""
)
else if ($NType = "_sparql_result:bnode" or $NType = "bnode")
then fn:concat("_:", $V)
else if ($NType = "_sparql_result:uri" or $NType = "uri")
then fn:concat("<", $V, ">")
else ""
return $rdf_term
};