-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract.xql
601 lines (454 loc) · 25.8 KB
/
extract.xql
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
xquery version "3.0";
(:~
: XTriples
:
: A generic webservice to extract rdf statements from XML resources
:
: @author Torsten Schrade
: @email <[email protected]>
: @version 1.4.0
: @licence MIT
:
: Main module containing the webservice
:)
(: ########## PROLOGUE ############################################################################# :)
import module namespace functx = "http://www.functx.com";
import module namespace config = "https://xtriples.lod.academy/config" at "modules/config.xqm";
declare namespace xtriples = "https://xtriples.lod.academy/";
declare variable $setConfiguration := xtriples:getConfiguration();
declare variable $setFormat := xtriples:getFormat();
(: ########## CONFIGURATION FUNCTIONS ############################################################## :)
(: retrieves the XTriples configuration from GET or POST :)
declare function xtriples:getConfiguration() {
(: checks GET/POST for a configuration sent with the configuration parameter :)
let $submittedConfiguration := request:get-parameter("configuration", "")
(: checks the request body for a configuration :)
let $submittedConfigurationPOSTBody := request:get-data()
let $setConfiguration :=
(: case 1 - URI to a configuration file was sent - GET request :)
if (substring($submittedConfiguration, 1, 4) = "http") then
fn:doc(xs:anyURI($submittedConfiguration))
(: case 2 - nothing submitted with a configuration parameter, use request body - direct POST request :)
else if ($submittedConfiguration = "") then
if ($submittedConfigurationPOSTBody) then
$submittedConfigurationPOSTBody
(: if no configuration could be retrieved to this point use a standard config that issues an error :)
else
fn:doc("configuration.xml")
(: case 3 - full configuration sent with the configuration parameter - form style POST request :)
else util:parse($submittedConfiguration)
return $setConfiguration
};
(: gets and sets the return format :)
declare function xtriples:getFormat() {
let $HEADER := request:get-header("format")
let $GET := request:get-parameter("format", "rdf")
let $setFormat :=
if ($HEADER != "") then $HEADER
else $GET
return $setFormat
};
(: ########## SANITIZATION FUNCTIONS ################################################################ :)
(: safety filter for XPATH/XQuery expressions - dissallows executions from dangerous/not needed function namespaces :)
declare function xtriples:expressionSanityCheck($expression as xs:string) as xs:boolean {
let $pattern := "((fn:.*\(.*\))|(doc*\(.*\))|(collection*\(.*\))|(v:.*\(.*\))|(backups:.*\(.*\))|(compression:.*\(.*\))|(contentextraction:.*\(.*\))|(counter:.*\(.*\))|(cqlparser:.*\(.*\))|(datetime:.*\(.*\))|(examples:.*\(.*\))|(exi:.*\(.*\))|(file:.*\(.*\))|(httpclient:.*\(.*\))|(image:.*\(.*\))|(inspection:.*\(.*\))|(jindi:.*\(.*\))|(kwic:.*\(.*\))|(lucene:.*\(.*\))|(mail:.*\(.*\))|(math:.*\(.*\))|(ngram:.*\(.*\))|(repo:.*\(.*\))|(request:.*\(.*\))|(response:.*\(.*\))|(scheduler:.*\(.*\))|(securitymanager:.*\(.*\))|(sequences:.*\(.*\))|(session:.*\(.*\))|(sort:.*\(.*\))|(sql:.*\(.*\))|(system:.*\(.*\))|(testing:.*\(.*\))|(text:.*\(.*\))|(transform:.*\(.*\))|(util:.*\(.*\))|(validation:.*\(.*\))|(xmldb:.*\(.*\))|(xmldiff:.*\(.*\))|(xqdoc:.*\(.*\))|(xslfo:.*\(.*\))|(config:.*\(.*\))|(docbook:.*\(.*\))|(app:.*\(.*\))|(dash:.*\(.*\))|(service:.*\(.*\))|(login-helper:.*\(.*\))|(packages:.*\(.*\))|(service:.*\(.*\))|(usermanager:.*\(.*\))|(demo:.*\(.*\))|(cex:.*\(.*\))|(ex:.*\(.*\))|(apputil:.*\(.*\))|(site:.*\(.*\))|(pretty:.*\(.*\))|(date:.*\(.*\))|(tei2:.*\(.*\))|(dbutil:.*\(.*\))|(docs:.*\(.*\))|(dq:.*\(.*\))|(review:.*\(.*\))|(epub:.*\(.*\))|(l18n:.*\(.*\))|(intl:.*\(.*\))|(restxq:.*\(.*\))|(tmpl:.*\(.*\))|(templates:.*\(.*\))|(trigger:.*\(.*\))|(jsjson:.*\(.*\))|(xqdoc:.*\(.*\)))"
let $check := matches($expression, $pattern)
return if ($check = true()) then false() else true()
};
(: ########## EXPRESSION EVALUATION FUNCTIONS ######################################################## :)
(: evaluates expressions in curly braces within URI strings :)
declare function xtriples:expressionBasedUriResolver($uri as xs:string, $currentResource as node(), $repeatIndex as xs:integer) as xs:string {
let $expression := concat('$currentResource', substring-after(substring-before($uri, "}"), "{"))
let $finalExpression :=
if (matches($expression, "\$repeatIndex")) then replace($expression, "\$repeatIndex", $repeatIndex)
else $expression
let $result :=
if (xtriples:expressionSanityCheck($finalExpression) = true()) then
try {
util:eval($finalExpression)
} catch * { $err:description }
else ""
let $uriWithSubstitution := if ($result) then replace($uri, "\{.*\}", $result) else ""
return $uriWithSubstitution
};
(: evaluates expressions in curly braces within attribute values (prepend, append, repeat) :)
declare function xtriples:expressionBasedAttributeResolver($currentResource as node(), $attributeValue as xs:string*, $repeatIndex as xs:integer, $resourceIndex as xs:integer) as xs:string* {
let $expression := substring-after(substring-before($attributeValue, "}"), "{")
let $expressionSubstitution :=
if (matches($expression, "\$resourceIndex")) then replace($expression, "\$resourceIndex", $resourceIndex)
else $expression
let $finalExpression :=
if (matches($expressionSubstitution, "\$repeatIndex")) then replace($expressionSubstitution, "\$repeatIndex", $repeatIndex)
else $expressionSubstitution
let $result :=
if ($finalExpression and xtriples:expressionSanityCheck($finalExpression) = true()) then
try {
replace($attributeValue, "\{.*\}", util:eval(concat("$currentResource", $finalExpression)))
} catch * { $err:description }
else $attributeValue
return $result
};
(: evaluates expressions in curly braces within a document string and retrieves the document :)
declare function xtriples:expressionBasedResourceResolver($collection as node()*, $resource as node()*) as item()* {
let $collectionContent := if (fn:doc-available($collection/@uri)) then fn:doc($collection/@uri) else ""
let $resourcesURI := string($resource/@uri)
let $resourcesExpression := concat('$collectionContent', substring-after(substring-before($resourcesURI, "}"), "{"))
let $resourcesNodes := if (xtriples:expressionSanityCheck($resourcesExpression) = true()) then
try { util:eval($resourcesExpression) } catch * { $err:description }
else $resourcesExpression
let $resources :=
for $resource at $index in $resourcesNodes
return
if ($resource instance of element()) then
$resource
else
element {"resource"} {
attribute {"uri"} {replace($resourcesURI, "\{.*\}", $resource)}
}
return $resources
};
(: ########## RESOURCE / COLLECTION FUNCTIONS ############################################################# :)
(: gets all resources for a collection, possibly expression based :)
declare function xtriples:getResources($collection as node()*) as item()* {
let $resources :=
for $resource in $collection/resource
return
if (matches($resource/@uri, "\{") and matches($resource/@uri, "\}")) then
xtriples:expressionBasedResourceResolver($collection, $resource)
else $resource
return $resources
};
(: ########## EXTRACTION FUNCTIONS ######################################################################### :)
(:
Separate relatively similar functions are used for subject, predicate and object extraction.
This keeps the query simpler even if it repeats some code. At the same time it opens up the possibility
to treat statement part extractions different in future versions (as it is already the case with subject/object
vs predicate extraction routines.
:)
(: extracts subject statements from the current resource :)
declare function xtriples:extractSubjects($currentResource as node(), $statement as node()*, $repeatIndex as xs:integer, $resourceIndex as xs:integer) as item()* {
let $externalResource :=
if (exists($statement/subject/@resource)) then
if (fn:doc-available(xtriples:expressionBasedUriResolver($statement/subject/@resource, $currentResource, $repeatIndex))) then
fn:doc(xtriples:expressionBasedUriResolver($statement/subject/@resource, $currentResource, $repeatIndex))
else ""
else ""
let $subjectExpressionConcatenation :=
if ($externalResource) then concat("$externalResource", string($statement/subject))
else concat('$currentResource', string($statement/subject))
let $subjectExpression :=
if (matches($subjectExpressionConcatenation, "\$repeatIndex")) then replace($subjectExpressionConcatenation, "$repeatIndex", $repeatIndex)
else $subjectExpressionConcatenation
let $subjectNodes := if (xtriples:expressionSanityCheck($subjectExpression) = true()) then
try { util:eval($subjectExpression) } catch * { $err:description }
else $subjectExpression
let $subjects :=
for $subjectValue in $subjectNodes
return functx:copy-attributes(<subject>{string($subjectValue)}</subject>, $statement/subject)
return $subjects
};
(: extracts predicate statements from the current resource :)
declare function xtriples:extractPredicate($currentResource as node(), $statement as node()*, $repeatIndex as xs:integer, $resourceIndex as xs:integer) as item()* {
let $externalResource :=
if (exists($statement/predicate/@resource)) then
if (fn:doc-available(xtriples:expressionBasedUriResolver($statement/predicate/@resource, $currentResource, $repeatIndex))) then
fn:doc(xtriples:expressionBasedUriResolver($statement/predicate/@resource, $currentResource, $repeatIndex))
else ""
else ""
let $predicateExpressionConcatenation :=
if ($externalResource) then concat("$externalResource", string($statement/predicate))
else concat('$currentResource', string($statement/predicate))
let $predicateExpression :=
if (matches($predicateExpressionConcatenation, "\$repeatIndex")) then replace($predicateExpressionConcatenation, "$repeatIndex", $repeatIndex)
else $predicateExpressionConcatenation
let $predicateValue := if (xtriples:expressionSanityCheck($predicateExpression) = true()) then
try { string(util:eval($predicateExpression)) } catch * { $err:description }
else $predicateExpression
let $predicate := <predicate prefix="{$statement/predicate/@prefix}">{string($predicateValue)}</predicate>
return $predicate
};
(: extracts object statements from the current resource :)
declare function xtriples:extractObjects($currentResource as node(), $statement as node()*, $repeatIndex as xs:integer, $resourceIndex as xs:integer) as item()* {
let $externalResource :=
if (exists($statement/object/@resource)) then
if (fn:doc-available(xtriples:expressionBasedUriResolver($statement/object/@resource, $currentResource, $repeatIndex))) then
fn:doc(xtriples:expressionBasedUriResolver($statement/object/@resource, $currentResource, $repeatIndex))
else ""
else ""
let $objectExpressionConcatenation :=
if ($externalResource) then concat("$externalResource", string($statement/object))
else concat("$currentResource", string($statement/object))
let $objectExpression :=
if (matches($objectExpressionConcatenation, "\$repeatIndex")) then replace($objectExpressionConcatenation, "$repeatIndex", $repeatIndex)
else $objectExpressionConcatenation
let $objectNodes := if (xtriples:expressionSanityCheck($objectExpression) = true()) then
try { util:eval($objectExpression) } catch * { $err:description }
else $objectExpression
let $objects :=
for $objectValue in $objectNodes
return functx:copy-attributes(<object>{string($objectValue)}</object>, $statement/object)
return $objects
};
(: XTriples core function - evaluates all configured statements for the current resource :)
declare function xtriples:extractTriples($currentResource as node(), $resourceIndex as xs:integer, $configuration as node()*) as item()* {
(: set the content of the current resource :)
let $currentResource :=
(: it can be a resource tag with an URI :)
if (xs:anyURI($currentResource/@uri) and fn:doc-available($currentResource/@uri)) then
fn:doc($currentResource/@uri)
(: or a resource tag with children :)
else if ($currentResource/*) then
$currentResource/*
else fn:doc("errors.xml")//error[@type='resource_no_content']
(: start statement pattern extraction :)
for $statement in $configuration//triples/*
let $repeat :=
if ($statement/@repeat) then
xs:integer(xtriples:expressionBasedAttributeResolver($currentResource, $statement/@repeat, 1, $resourceIndex))
else 1
for $repeatIndex in (1 to $repeat)
(: if a condition expression is given in the current statement, evaluate it :)
let $condition :=
if (exists($statement/condition)) then
if (xtriples:expressionSanityCheck(string($statement/condition)) = true()) then
try { boolean(util:eval(concat('$currentResource', string($statement/condition)))) } catch * { $err:description }
else true()
else true()
(: n possible subjects per statement declaration :)
let $subjects :=
if (starts-with(string($statement/subject), '/')) then
xtriples:extractSubjects($currentResource, $statement, $repeatIndex, $resourceIndex)
else $statement/subject
(: 1 predicate per statement declaration :)
let $predicate :=
if (starts-with(string($statement/predicate), '/')) then
xtriples:extractPredicate($currentResource, $statement, $repeatIndex, $resourceIndex)
else <predicate prefix="{$statement/predicate/@prefix}">{string($statement/predicate)}</predicate>
(: n possible objects per statement declaration :)
let $objects :=
if (starts-with(string($statement/object), '/')) then
xtriples:extractObjects($currentResource, $statement, $repeatIndex, $resourceIndex)
else $statement/object
(: build statements - but only if the condition expression returned any value - boolean, string, node set etc. :)
let $statements :=
if ($condition) then
for $subject in $subjects
let $subjectReturn :=
if ($subject = "") then "" else
for $object in $objects
let $subjectPrepend := xtriples:expressionBasedAttributeResolver($currentResource, $subject/@prepend, $repeatIndex, $resourceIndex)
let $subjectAppend := xtriples:expressionBasedAttributeResolver($currentResource, $subject/@append, $repeatIndex, $resourceIndex)
let $predicatePrepend := xtriples:expressionBasedAttributeResolver($currentResource, $predicate/@prepend, $repeatIndex, $resourceIndex)
let $predicateAppend := xtriples:expressionBasedAttributeResolver($currentResource, $predicate/@append, $repeatIndex, $resourceIndex)
let $objectPrepend := xtriples:expressionBasedAttributeResolver($currentResource, $object/@prepend, $repeatIndex, $resourceIndex)
let $objectAppend := xtriples:expressionBasedAttributeResolver($currentResource, $object/@append, $repeatIndex, $resourceIndex)
let $objectReturn :=
if ($object = "") then ""
else <statement>{(
functx:remove-attributes(functx:copy-attributes(<subject>{concat($subjectPrepend, $subject, $subjectAppend)}</subject>, $subject), ('append', 'prepend')),
functx:remove-attributes(functx:copy-attributes(<predicate>{concat($predicatePrepend, $predicate, $predicateAppend)}</predicate>, $predicate), ('append', 'prepend')),
functx:remove-attributes(functx:copy-attributes(<object>{concat($objectPrepend, $object, $objectAppend)}</object>, $object), ('append', 'prepend'))
)}</statement>
return $objectReturn
return $subjectReturn
else ""
return $statements
};
(: ########## FORMAT FUNCTIONS #################################################################################### :)
(: internal/intermediate XTriples RDF format, one RDF node per XTriples statement - will later be condensed by the any23 webservice to "real" RDF :)
declare function xtriples:generateRDFTriples($xtriples as node()*) as item()* {
for $statement in $xtriples//statements/statement
let $subjectType := $statement/subject/@type
let $subjectPrefix := $statement/subject/@prefix
let $subjectUri := $xtriples//vocabularies/vocabulary[@prefix=$subjectPrefix]/@uri
let $subjectValue := $statement/subject
let $predicatePrefix := $statement/predicate/@prefix
let $predicateUri := $xtriples//vocabularies/vocabulary[@prefix=$predicatePrefix]/@uri
let $predicateValue := $statement/predicate
let $objectType := $statement/object/@type
let $objectPrefix := $statement/object/@prefix
let $objectUri := $xtriples//vocabularies/vocabulary[@prefix=$objectPrefix]/@uri
let $objectValue := $statement/object/text()
(: rdf triple construction using computed constructors with qualified names; builds single rdf triples which are later combined by any23 webservice :)
let $triple :=
(: outer RDF tag :)
element {QName("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:Description")} {
(: either is a blank node, then gets rdf:nodeID attribute :)
(if ($subjectType = 'bnode') then
attribute {QName("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:nodeID")} { $subjectValue }
(: or is a URI, then gets rdf:about attribute :)
else
attribute {QName("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:about")} { concat($subjectUri, $subjectValue) }),
(: predicate = inner element of the RDF tag :)
element {QName($predicateUri, concat($predicatePrefix, ":", $predicateValue))} {
(: object uri; set rdf:resource as attribute to predicate :)
if ($objectType = 'uri') then
attribute {QName("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:resource")} {
if ($objectPrefix) then concat($objectUri, $objectValue) else $objectValue
}
(: object blank node; set rdf:nodeIFD as attribute to predicate :)
else if ($objectType = 'bnode') then
attribute {QName("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:nodeID")} { $objectValue }
(: typed object literals; append rdf:datatype as attribute and set value as text node :)
else if ($statement/object/@datatype) then
(attribute {QName("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:datatype")} { concat("http://www.w3.org/2001/XMLSchema#", $statement/object/@datatype) }, $objectValue)
(: language tagged object literals; append rdf:datatype as attribute and set value as text node :)
else if ($statement/object/@lang) then
(attribute {"xml:lang"} { $statement/object/@lang }, $objectValue)
(: plain object literals; set value as text node :)
else
$objectValue
}
}
return
$triple
};
(: gets the internal RDF format :)
declare function xtriples:getRDFTriples($xtriples as node()*, $vocabularies as node()*) as item()* {
let $rdfTriples := <rdftriples>{$vocabularies}{xtriples:generateRDFTriples($xtriples)}</rdftriples>
return $rdfTriples
};
(: gets the internal RDF format and sends it to any23 for further transformation :)
declare function xtriples:getRDF($xtriples as node()*, $vocabularies as node()*) as item()* {
(: internal RDF format :)
let $rdfstylesheet := doc("rdf.xsl")
let $rdfTriples := xtriples:getRDFTriples($xtriples, $vocabularies)
let $rdfInternal := transform:transform($rdfTriples, $rdfstylesheet, ())
(: official RDF format via any23 :)
let $headers := <headers><header name="Content-Type" value="application/rdf+xml; charset=UTF-8"/></headers>
let $POST_request := httpclient:post(xs:anyURI(concat($config:any23WebserviceURL, "rdfxml")), $rdfInternal, false(), $headers)
let $rdf := $POST_request//httpclient:body/*
return $rdf
};
(: gets ntriples format from any23 by sending in extracted RDF :)
declare function xtriples:getNTRIPLES($rdf as node()*) as item()* {
(: url encoded ntriples :)
let $headers := <headers><header name="Content-Type" value="application/rdf+xml; charset=UTF-8"/></headers>
let $POST_request := httpclient:post(xs:anyURI(concat($config:any23WebserviceURL, "nt")), $rdf, false(), $headers)
let $ntriples := util:unescape-uri(replace(string($POST_request//httpclient:body), '%00', ''), "UTF-8")
return $ntriples
};
(: gets turtle format from any23 by sending in extracted RDF :)
declare function xtriples:getTURTLE($rdf as node()*) as item()* {
(: eXist returns base64Binary turtle :)
let $headers := <headers><header name="Content-Type" value="application/rdf+xml; charset=UTF-8"/></headers>
let $POST_request := httpclient:post(xs:anyURI(concat($config:any23WebserviceURL, "turtle")), $rdf, false(), $headers)
let $turtle := util:binary-to-string(xs:base64Binary($POST_request//httpclient:body), "UTF-8")
return $turtle
};
(: gets nquads format from any23 by sending in extracted RDF :)
declare function xtriples:getNQUADS($rdf as node()*) as item()* {
(: eXist returns base64Binary nquads :)
let $headers := <headers><header name="Content-Type" value="application/rdf+xml; charset=UTF-8"/></headers>
let $POST_request := httpclient:post(xs:anyURI(concat($config:any23WebserviceURL, "nq")), $rdf, false(), $headers)
let $nquads := util:binary-to-string(xs:base64Binary($POST_request//httpclient:body), "UTF-8")
return $nquads
};
(: gets json format from any23 by sending in extracted RDF :)
declare function xtriples:getJSON($rdf as node()*) as item()* {
(: eXist returns base64Binary json :)
let $headers := <headers><header name="Content-Type" value="application/rdf+xml; charset=UTF-8"/></headers>
let $POST_request := httpclient:post(xs:anyURI(concat($config:any23WebserviceURL, "json")), $rdf, false(), $headers)
let $json := util:binary-to-string(xs:base64Binary($POST_request//httpclient:body), "UTF-8")
return $json
};
(: gets trix format from any23 by sending in extracted RDF :)
declare function xtriples:getTRIX($rdf as node()*) as item()* {
(: eXist returns base64Binary json :)
let $headers := <headers><header name="Content-Type" value="application/rdf+xml; charset=UTF-8"/></headers>
let $POST_request := httpclient:post(xs:anyURI(concat($config:any23WebserviceURL, "trix")), $rdf, false(), $headers)
let $trix := $POST_request//httpclient:body/*
return $trix
};
(: gets svg format from rhizomik webservice by temporarily storing the extracted RDF and pointing the redefer to the temporary file :)
declare function xtriples:getSVG($rdf as node()*) as item()* {
(: svg format with temporary file :)
let $filename := concat(util:uuid(), ".xml")
let $store := xmldb:store("/db/apps/xtriples/temp/", $filename, $rdf)
let $svgHeaders :=
<headers>
<header name="format" value="RDF/XML"/>
<header name="mode" value="svg" />
<headers name="rules" value="{$config:redeferWebserviceRulesURL}" />
</headers>
let $GET_request := httpclient:get(xs:anyURI(concat($config:redeferWebserviceURL, "render?rdf=", $config:xtriplesWebserviceURL, "temp/", $filename, "&format=RDF/XML&mode=svg&rules=", $config:redeferWebserviceRulesURL)), false(), $svgHeaders)
let $svg := $GET_request//httpclient:body/*
let $delete := xmldb:remove("/db/apps/xtriples/temp/", $filename)
return $svg
};
(: ########## MAIN QUERY BODY ############################################################################################### :)
(: set basic vars :)
let $collections := $setConfiguration/xtriples/collection
let $configuration := $setConfiguration/xtriples/configuration
let $vocabularies := $configuration/vocabularies
(: dynamic namespace declaration for all configured vocabularies :)
let $namespaces :=
if ($configuration/vocabularies/*) then
for $namespace in $configuration/vocabularies/*
return util:declare-namespace($namespace/@prefix, $namespace/@uri)
else ""
(: extract triples from collections :)
let $extraction :=
for $collection in $collections
let $maxResources :=
if ($collection/@max > 0) then
$collection/@max
else 0
let $resources := xtriples:getResources($collection)
let $triples :=
for $resource at $resourceIndex in $resources
let $currentResource :=
if (name($resource) = 'resource') then
$resource
else <resource>{$resource}</resource>
return
if ($maxResources > 0 and $resourceIndex <= $maxResources)
then
<statements>{xtriples:extractTriples($currentResource, $resourceIndex, $configuration)}</statements>
else if ($maxResources = 0)
then
<statements>{xtriples:extractTriples($currentResource, $resourceIndex, $configuration)}</statements>
else ""
return <result>{(functx:copy-attributes(<collection>{$resources}</collection>, $collection),<triples>{$triples}</triples>)}</result>
(: construct internal result format :)
let $xtriples :=
<xtriples>
<configuration>
{$vocabularies}
<triples>{$extraction//triples/*}</triples>
</configuration>
{$extraction//collection}
</xtriples>
(: transform and return result :)
return (
if ($setFormat = "xtriples") then
$xtriples
else if ($setFormat = "rdftriples") then
xtriples:getRDFTriples($xtriples, $vocabularies)
else if ($setFormat = "ntriples") then (
response:set-header("Content-Type", "text/plain; charset=UTF-8"),
response:stream(xtriples:getNTRIPLES(xtriples:getRDF($xtriples, $vocabularies)), "method=text")
)
else if ($setFormat = "turtle") then (
response:set-header("Content-Type", "text/turtle; charset=UTF-8"),
response:stream(xtriples:getTURTLE(xtriples:getRDF($xtriples, $vocabularies)), "method=text")
)
else if ($setFormat = "nquads") then (
response:set-header("Content-Type", "text/x-nquads; charset=UTF-8"),
response:stream(xtriples:getNQUADS(xtriples:getRDF($xtriples, $vocabularies)), "method=text")
)
else if ($setFormat = "json") then (
response:set-header("Content-Type", "text/json; charset=UTF-8"),
response:stream(xtriples:getJSON(xtriples:getRDF($xtriples, $vocabularies)), "method=text")
)
else if ($setFormat = "trix") then (
response:set-header("Content-Type", "application/trix; charset=UTF-8"),
xtriples:getTRIX(xtriples:getRDF($xtriples, $vocabularies))
)
else if ($setFormat = "svg") then
(: response:set-header("Content-Type", "image/svg+xml; charset=UTF-8"), :)
xtriples:getSVG(xtriples:getRDF($xtriples, $vocabularies))
else
(: response:set-header("Content-Type", "application/rdf+xml; charset=UTF-8"), :)
xtriples:getRDF($xtriples, $vocabularies)
)