ld4l-works_rdf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +25 -0
- data/.travis.yml +14 -0
- data/CHANGES.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +170 -0
- data/Rakefile +2 -0
- data/ld4l-works_rdf.gemspec +42 -0
- data/lib/ld4l/works_rdf.rb +131 -0
- data/lib/ld4l/works_rdf/configuration.rb +41 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_identifier.rb +17 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_instance.rb +23 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_organization.rb +16 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_person.rb +16 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_place.rb +16 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_provider.rb +18 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_title.rb +18 -0
- data/lib/ld4l/works_rdf/models/bibframe/bibframe_work.rb +16 -0
- data/lib/ld4l/works_rdf/models/bibo/bibo_book.rb +19 -0
- data/lib/ld4l/works_rdf/models/bibo/bibo_document.rb +14 -0
- data/lib/ld4l/works_rdf/models/bibo/vivo_authorship.rb +15 -0
- data/lib/ld4l/works_rdf/models/bibo/vivo_book.rb +18 -0
- data/lib/ld4l/works_rdf/models/generic_work.rb +13 -0
- data/lib/ld4l/works_rdf/models/schema/oclc_schema_book.rb +16 -0
- data/lib/ld4l/works_rdf/models/schema/schema_book.rb +23 -0
- data/lib/ld4l/works_rdf/models/schema/schema_person.rb +17 -0
- data/lib/ld4l/works_rdf/models/schema/schema_publisher.rb +15 -0
- data/lib/ld4l/works_rdf/models/work_metadata.rb +212 -0
- data/lib/ld4l/works_rdf/services/attempt_generic_metadata_extraction.rb +41 -0
- data/lib/ld4l/works_rdf/services/conversion_services/get_rdfxml_from_marcxml.rb +44 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.ErrorCodes.xqy +56 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MARCXML-2-MADSRDF.xqy +1702 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MARCXML-2-RecordInfoRDF.xqy +216 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MARCXMLBIB-2-BIBFRAME.xqy +140 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MARCXMLBIB-BFUtils.xqy +3287 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MBIB-2-BIBFRAME-Shared.xqy +4112 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MBIB-Default-2-BF.xqy +61 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.MBIB-NotatedMusic-2-BF.xqy +105 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.RDFXML-2-ExhibitJSON.xqy +119 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.RDFXML-2-JSON.xqy +193 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.RDFXML-2-Ntriples.xqy +276 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.RDFXMLnested-2-flat.xqy +380 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/xbin/ml.xqy +239 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/xbin/saxon.xqy +134 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/xbin/zorba.xqy +359 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/xbin/zorba2-0.xqy +249 -0
- data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/xbin/zorba3-0.xqy +362 -0
- data/lib/ld4l/works_rdf/services/conversion_services/saxon/saxon9he.jar +0 -0
- data/lib/ld4l/works_rdf/services/get_metadata_from_marcxml_uri.rb +43 -0
- data/lib/ld4l/works_rdf/services/get_metadata_from_oclc_uri.rb +25 -0
- data/lib/ld4l/works_rdf/services/get_metadata_from_solr_query.rb +29 -0
- data/lib/ld4l/works_rdf/services/get_metadata_from_vivo_uri.rb +25 -0
- data/lib/ld4l/works_rdf/services/get_model_from_uri.rb +62 -0
- data/lib/ld4l/works_rdf/services/metadata_services/get_metadata_from_bibframe_models.rb +60 -0
- data/lib/ld4l/works_rdf/services/metadata_services/get_metadata_from_bibo_model.rb +42 -0
- data/lib/ld4l/works_rdf/services/metadata_services/get_metadata_from_generic_model.rb +41 -0
- data/lib/ld4l/works_rdf/services/metadata_services/get_metadata_from_oclc_model.rb +42 -0
- data/lib/ld4l/works_rdf/services/metadata_services/get_metadata_from_solr_doc.rb +67 -0
- data/lib/ld4l/works_rdf/services/metadata_services/get_metadata_from_vivo_model.rb +45 -0
- data/lib/ld4l/works_rdf/services/metadata_services/set_error_in_metadata.rb +27 -0
- data/lib/ld4l/works_rdf/services/model_services/populate_bibframe_models_from_repository.rb +46 -0
- data/lib/ld4l/works_rdf/services/model_services/populate_generic_model_from_repository.rb +30 -0
- data/lib/ld4l/works_rdf/services/model_services/populate_oclc_model_from_repository.rb +27 -0
- data/lib/ld4l/works_rdf/services/model_services/populate_vivo_model_from_repository.rb +27 -0
- data/lib/ld4l/works_rdf/services/negotiation_services/get_marcxml_from_uri.rb +35 -0
- data/lib/ld4l/works_rdf/services/negotiation_services/get_rdfxml_from_uri.rb +37 -0
- data/lib/ld4l/works_rdf/services/negotiation_services/get_solr_results_from_solr_query.rb +35 -0
- data/lib/ld4l/works_rdf/services/negotiation_services/get_turtle_from_uri.rb +37 -0
- data/lib/ld4l/works_rdf/services/negotiation_services/response_header.rb +51 -0
- data/lib/ld4l/works_rdf/services/repository_services/populate_graph_from_rdfxml.rb +22 -0
- data/lib/ld4l/works_rdf/services/repository_services/populate_graph_from_turtle.rb +21 -0
- data/lib/ld4l/works_rdf/services/repository_services/populate_repository_from_graph.rb +25 -0
- data/lib/ld4l/works_rdf/version.rb +5 -0
- data/lib/ld4l/works_rdf/vocab/bf.rb +29 -0
- data/lib/ld4l/works_rdf/vocab/bgn.rb +5 -0
- data/lib/ld4l/works_rdf/vocab/bibo.rb +10 -0
- data/lib/ld4l/works_rdf/vocab/library.rb +6 -0
- data/lib/ld4l/works_rdf/vocab/vitro.rb +5 -0
- data/lib/ld4l/works_rdf/vocab/vivo.rb +11 -0
- data/spec/ld4l/works_rdf/configuration_spec.rb +166 -0
- data/spec/ld4l/works_rdf/models/books/vivo_book_rdf_spec.rb +267 -0
- data/spec/ld4l/works_rdf/services/get_metadata_from_uri_spec.rb +39 -0
- data/spec/ld4l/works_rdf/services/get_model_from_uri_spec.rb +34 -0
- data/spec/ld4l/works_rdf_spec.rb +53 -0
- data/spec/spec_helper.rb +26 -0
- metadata +321 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
xquery version "1.0";
|
2
|
+
(:
|
3
|
+
: Module Name: MARCXML BIB to bibframe
|
4
|
+
:
|
5
|
+
: Module Version: 1.0
|
6
|
+
:
|
7
|
+
: Date: 2014 Jan 30
|
8
|
+
:
|
9
|
+
: Copyright: Public Domain
|
10
|
+
:
|
11
|
+
: Proprietary XQuery Extensions Used: None
|
12
|
+
:
|
13
|
+
: Xquery Specification: January 2007
|
14
|
+
:
|
15
|
+
: Module Overview: Transforms special kinds of MARC Bib records
|
16
|
+
: into their bibframe parts.
|
17
|
+
:
|
18
|
+
:)
|
19
|
+
|
20
|
+
(:~
|
21
|
+
: This module handles transformations of specific types of marc records, starting with Music (audio and notated)
|
22
|
+
: It is called from MarcxmlBib-2-Bibframe, and calls generic functions from that module.
|
23
|
+
:
|
24
|
+
: Modified to receive $collection, consisting of a bib and it's holdings (optionally)
|
25
|
+
:
|
26
|
+
: @author Kevin Ford (kefo@loc.gov)
|
27
|
+
: @author Nate Trail (ntra@loc.gov)
|
28
|
+
: @since December 16, 2014
|
29
|
+
: @version 1.0
|
30
|
+
:)
|
31
|
+
|
32
|
+
module namespace bfdefault = "info:lc/id-modules/marcdefault2bf#";
|
33
|
+
|
34
|
+
(: MODULES :)
|
35
|
+
|
36
|
+
import module namespace marc2bfutils = "info:lc/id-modules/marc2bfutils#" at "module.MARCXMLBIB-BFUtils.xqy";
|
37
|
+
import module namespace mbshared = "info:lc/id-modules/mbib2bibframeshared#" at "module.MBIB-2-BIBFRAME-Shared.xqy";
|
38
|
+
|
39
|
+
(: NAMESPACES :)
|
40
|
+
declare namespace marcxml = "http://www.loc.gov/MARC21/slim";
|
41
|
+
declare namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
42
|
+
declare namespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";
|
43
|
+
|
44
|
+
declare namespace bf = "http://bibframe.org/vocab/";
|
45
|
+
declare namespace madsrdf = "http://www.loc.gov/mads/rdf/v1#";
|
46
|
+
declare namespace relators = "http://id.loc.gov/vocabulary/relators/";
|
47
|
+
declare namespace hld = "http://www.loc.gov/opacxml/holdings/" ;
|
48
|
+
|
49
|
+
(:~
|
50
|
+
: This is the function that generates bibframe data from any resource not otherwise handled.
|
51
|
+
:
|
52
|
+
: @param $marcxml element is the MARCXML
|
53
|
+
: @return bf:* as element()
|
54
|
+
:)
|
55
|
+
declare function bfdefault:generate-default-work(
|
56
|
+
$collection as element(marcxml:collection),
|
57
|
+
$workID as xs:string
|
58
|
+
) as element () {
|
59
|
+
mbshared:generate-work($collection, $workID)
|
60
|
+
|
61
|
+
};
|
@@ -0,0 +1,105 @@
|
|
1
|
+
xquery version "1.0";
|
2
|
+
(:
|
3
|
+
: Module Name: MARCXML BIB to bibframe
|
4
|
+
:
|
5
|
+
: Module Version: 1.0
|
6
|
+
:
|
7
|
+
: Date: 2014 Jan 30
|
8
|
+
:
|
9
|
+
: Copyright: Public Domain
|
10
|
+
:
|
11
|
+
: Proprietary XQuery Extensions Used: None
|
12
|
+
:
|
13
|
+
: Xquery Specification: January 2007
|
14
|
+
:
|
15
|
+
: Module Overview: Transforms special kinds of MARC Bib records
|
16
|
+
: into their bibframe parts.
|
17
|
+
:
|
18
|
+
:)
|
19
|
+
|
20
|
+
(:~
|
21
|
+
: This module handles transformations of specific types of marc records, starting with Music (audio and notated)
|
22
|
+
: It is called from MarcxmlBib-2-Bibframe, and calls generic functions from that module.
|
23
|
+
:
|
24
|
+
: Modified to receive $collection, consisting of a bib and it's holdings (optionally)
|
25
|
+
:
|
26
|
+
: @author Kevin Ford (kefo@loc.gov)
|
27
|
+
: @author Nate Trail (ntra@loc.gov)
|
28
|
+
: @since December 16, 2014
|
29
|
+
: @version 1.0
|
30
|
+
:)
|
31
|
+
|
32
|
+
module namespace music = "info:lc/id-modules/marcnotatedmusic2bf#";
|
33
|
+
|
34
|
+
(: MODULES :)
|
35
|
+
|
36
|
+
import module namespace marc2bfutils = "info:lc/id-modules/marc2bfutils#" at "module.MARCXMLBIB-BFUtils.xqy";
|
37
|
+
import module namespace mbshared = 'info:lc/id-modules/mbib2bibframeshared#' at "module.MBIB-2-BIBFRAME-Shared.xqy";
|
38
|
+
|
39
|
+
(: NAMESPACES :)
|
40
|
+
declare namespace marcxml = "http://www.loc.gov/MARC21/slim";
|
41
|
+
declare namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
42
|
+
declare namespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";
|
43
|
+
|
44
|
+
declare namespace bf = "http://bibframe.org/vocab/";
|
45
|
+
declare namespace madsrdf = "http://www.loc.gov/mads/rdf/v1#";
|
46
|
+
declare namespace relators = "http://id.loc.gov/vocabulary/relators/";
|
47
|
+
declare namespace hld = "http://www.loc.gov/opacxml/holdings/" ;
|
48
|
+
|
49
|
+
(:~
|
50
|
+
: This is the function that generates a notated music work resource.
|
51
|
+
: For now, just call the main function.
|
52
|
+
:
|
53
|
+
: @param $marcxml element is the MARCXML
|
54
|
+
: @return bf:* as element()
|
55
|
+
:)
|
56
|
+
declare function music:generate-notatedmusic-work(
|
57
|
+
$collection as element(marcxml:collection),
|
58
|
+
$workID as xs:string
|
59
|
+
) as element () {
|
60
|
+
mbshared:generate-work( $collection, $workID )
|
61
|
+
};
|
62
|
+
|
63
|
+
(:~
|
64
|
+
: This is the function that generates a notated music work resource.
|
65
|
+
:
|
66
|
+
: @param $marcxml element is the MARCXML
|
67
|
+
: @return bf:* as element()
|
68
|
+
:)
|
69
|
+
|
70
|
+
(:declare function music:generate-notatedmusic-work(
|
71
|
+
$marcxml as element(marcxml:record),
|
72
|
+
$workID as xs:string
|
73
|
+
) as element () {
|
74
|
+
let $instances := mbshared:generate-instances($marcxml, $workID)
|
75
|
+
|
76
|
+
let $types := mbshared:get-resourceTypes($marcxml)
|
77
|
+
|
78
|
+
let $mainType := "Work"
|
79
|
+
let $biblink:=fn:concat(
|
80
|
+
$workID,
|
81
|
+
fn:normalize-space(fn:string($marcxml/marcxml:controlfield[@tag eq "001"]))
|
82
|
+
)
|
83
|
+
let $derivedFrom :=
|
84
|
+
element bf:derivedFrom {
|
85
|
+
attribute rdf:resource {
|
86
|
+
fn:concat($biblink, ".marcxml.xml")
|
87
|
+
}
|
88
|
+
}
|
89
|
+
let $subjects:=
|
90
|
+
for $d in $marcxml/marcxml:datafield[fn:matches(fn:string-join($marc2bfutils:subject-types//@tag," "),fn:string(@tag))]
|
91
|
+
return mbshared:get-subject($d)
|
92
|
+
return
|
93
|
+
element {fn:concat("bf:" , $mainType)} {
|
94
|
+
attribute rdf:about {$workID},
|
95
|
+
for $t in fn:distinct-values($types)
|
96
|
+
return
|
97
|
+
element rdf:type {
|
98
|
+
attribute rdf:resource {fn:concat("http://bibframe.org/vocab/", $t)}
|
99
|
+
},
|
100
|
+
$subjects,
|
101
|
+
$derivedFrom,
|
102
|
+
for $i in $instances
|
103
|
+
return element bf:hasInstance{$i}
|
104
|
+
}
|
105
|
+
};:)
|
@@ -0,0 +1,119 @@
|
|
1
|
+
xquery version "1.0";
|
2
|
+
|
3
|
+
(:
|
4
|
+
: Module Name: BIBFRAME RDF/XML 2 JSON for Exhibit
|
5
|
+
:
|
6
|
+
: Module Version: 1.0
|
7
|
+
:
|
8
|
+
: Date: 2013 10 Jan
|
9
|
+
:
|
10
|
+
: Copyright: Public Domain
|
11
|
+
:
|
12
|
+
: Proprietary XQuery Extensions Used: none
|
13
|
+
:
|
14
|
+
: Xquery Specification: January 2007
|
15
|
+
:
|
16
|
+
: Module Overview: Takes BIBFRAME RDF/XML and converts
|
17
|
+
: to JSON serialization to be used with MIT Exhibit.
|
18
|
+
: This may be a tortured train wreck. We'll see.
|
19
|
+
:
|
20
|
+
:)
|
21
|
+
|
22
|
+
(:~
|
23
|
+
: Takes BIBFRAME RDF/XML and converts
|
24
|
+
: to JSON serialization to be used with MIT Exhibit.
|
25
|
+
:
|
26
|
+
: @author Kevin Ford (kefo@loc.gov)
|
27
|
+
: @since January 10, 2013
|
28
|
+
: @version 1.0
|
29
|
+
:)
|
30
|
+
|
31
|
+
module namespace bfRDFXML2exhibitJSON = 'info:lc/bf-modules/bfRDFXML2exhibitJSON#';
|
32
|
+
|
33
|
+
declare namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
34
|
+
declare namespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";
|
35
|
+
|
36
|
+
declare namespace bf = "http://bibframe.org/vocab/";
|
37
|
+
declare namespace madsrdf = "http://www.loc.gov/mads/rdf/v1#";
|
38
|
+
declare namespace relators = "http://id.loc.gov/vocabulary/relators/";
|
39
|
+
declare namespace identifiers = "http://id.loc.gov/vocabulary/identifiers/";
|
40
|
+
declare namespace notes = "http://id.loc.gov/vocabulary/notes/";
|
41
|
+
|
42
|
+
|
43
|
+
(:~
|
44
|
+
: This is the main function. It converts BIBFRAME RDF/XML
|
45
|
+
: to JSON serialization to be used with MIT Exhibit.
|
46
|
+
:
|
47
|
+
: @param $rdfxml node() is the RDF/XML
|
48
|
+
: @param $baseuri xs:string is the base uri for identifiers
|
49
|
+
: @param $hash xs:string a unique hash for the identifier
|
50
|
+
: it is designed to be used when this script
|
51
|
+
: is run as part of a larger environment.
|
52
|
+
: @return xs:string javascript
|
53
|
+
:)
|
54
|
+
declare function bfRDFXML2exhibitJSON:bfRDFXML2exhibitJSON
|
55
|
+
(
|
56
|
+
$rdfxml as element(rdf:RDF),
|
57
|
+
$baseuri as xs:string
|
58
|
+
)
|
59
|
+
as xs:string
|
60
|
+
{
|
61
|
+
|
62
|
+
let $resources :=
|
63
|
+
for $c in $rdfxml/*[fn:name()]
|
64
|
+
let $type := fn:local-name($c)
|
65
|
+
let $uri := xs:string($c/@rdf:about)
|
66
|
+
let $id := fn:replace($uri, $baseuri, "")
|
67
|
+
let $l := ($c/bf:authorizedAccessPoint|$c/bf:label|$c/bf:title|$c/madsrdf:authoritativeLabel|$c/rdfs:label)[1]
|
68
|
+
let $l := fn:replace(xs:string($l), '"', '\\"')
|
69
|
+
let $props-names :=
|
70
|
+
for $p in $c/*[fn:name()]
|
71
|
+
return fn:name($p)
|
72
|
+
let $props-distinct := fn:distinct-values($props-names)
|
73
|
+
let $props :=
|
74
|
+
for $name in $props-distinct
|
75
|
+
let $ps :=
|
76
|
+
for $p in $c/*[fn:name()=$name]
|
77
|
+
return
|
78
|
+
if ($p/@rdf:resource) then
|
79
|
+
fn:concat('"', fn:replace(xs:string($p/@rdf:resource), $baseuri, "") , '"')
|
80
|
+
else if ($p/madsrdf:authoritativeLabel or $p/bf:label or $p/rdfs:label) then
|
81
|
+
let $label := ($p/madsrdf:authoritativeLabel|$p/bf:label|$p/rdfs:label|$p/bf:title)[1]
|
82
|
+
return fn:concat('"', fn:replace(xs:string($label), '"', '\\"') , '"')
|
83
|
+
(: else if (xs:string($p/@rdf:parseType)="Collection") then :)
|
84
|
+
else if ($p/child::node()[fn:name()]/child::node()[fn:name()]) then
|
85
|
+
let $list-items :=
|
86
|
+
for $i in $p/*[fn:name()]
|
87
|
+
let $t := fn:local-name($i)
|
88
|
+
let $els :=
|
89
|
+
for $e in $i/*[fn:name()]
|
90
|
+
let $n := fn:replace(fn:name($e), ":", "-")
|
91
|
+
return fn:concat('"', $n , '": "', fn:normalize-space(fn:replace(xs:string($e), '"', '\\"')), '"')
|
92
|
+
return
|
93
|
+
fn:concat('{
|
94
|
+
"type": ', fn:concat('"', $t , '"'), ',
|
95
|
+
', fn:string-join($els, ", "), '
|
96
|
+
}')
|
97
|
+
return fn:concat('[', fn:string-join($list-items, ', '), ']')
|
98
|
+
else
|
99
|
+
fn:concat('"', fn:normalize-space(fn:replace(xs:string($p), '"', '\\"')) , '"')
|
100
|
+
let $ps :=
|
101
|
+
if ( fn:count($ps) eq 1 ) then
|
102
|
+
fn:concat('"', fn:replace($name, ":", "-") , '": ', $ps)
|
103
|
+
else
|
104
|
+
fn:concat('"', fn:replace($name, ":", "-") , '": [', fn:string-join($ps, ', '), ']' )
|
105
|
+
where xs:string($name) ne "rdf:type"
|
106
|
+
return $ps
|
107
|
+
return
|
108
|
+
fn:concat('{
|
109
|
+
"type": ', fn:concat('"', $type , '"'), ',
|
110
|
+
"id": ', fn:concat('"', $id , '"'), ',
|
111
|
+
"bf-id": ', fn:concat('"', $id , '"'), ',
|
112
|
+
"uri": ', fn:concat('"', $uri , '"'), ',
|
113
|
+
"label": ', fn:concat('"', $l , '"'), ',
|
114
|
+
', fn:string-join($props, ", "), '
|
115
|
+
}')
|
116
|
+
|
117
|
+
return fn:concat('{"items": [' , fn:string-join($resources, ", "), ']}')
|
118
|
+
|
119
|
+
};
|
data/lib/ld4l/works_rdf/services/conversion_services/marc2bibframe/modules/module.RDFXML-2-JSON.xqy
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
xquery version "1.0";
|
2
|
+
|
3
|
+
(:
|
4
|
+
: Module Name: RDF 2 JSON
|
5
|
+
:
|
6
|
+
: Module Version: 1.0
|
7
|
+
:
|
8
|
+
: Date: 2011 June 13
|
9
|
+
:
|
10
|
+
: Copyright: Public Domain
|
11
|
+
:
|
12
|
+
: Proprietary XQuery Extensions Used: none
|
13
|
+
:
|
14
|
+
: Xquery Specification: January 2007
|
15
|
+
:
|
16
|
+
: Module Overview: Takes NTriples and converts
|
17
|
+
: to JSON serialization.
|
18
|
+
:
|
19
|
+
:)
|
20
|
+
|
21
|
+
(:~
|
22
|
+
: Takes NTriples and converts
|
23
|
+
: to JSON serialization.
|
24
|
+
:
|
25
|
+
: @author Kevin Ford (kefo@loc.gov)
|
26
|
+
: @since June 13, 2011
|
27
|
+
: @version 1.0
|
28
|
+
:)
|
29
|
+
|
30
|
+
module namespace rdfxml2json = 'info:lc/id-modules/rdfxml2json#';
|
31
|
+
|
32
|
+
(: Imported Modules :)
|
33
|
+
import module namespace rdfxml2nt = "info:lc/id-modules/rdfxml2nt#" at "module.RDFXML-2-Ntriples.xqy";
|
34
|
+
|
35
|
+
(: Namespace(s) :)
|
36
|
+
declare namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
37
|
+
declare namespace madsrdf = "http://www.loc.gov/mads/rdf/v1#";
|
38
|
+
|
39
|
+
|
40
|
+
(:~
|
41
|
+
: This is the main function. It converts full RDF/XML to
|
42
|
+
: Protovis's Javascript for a force-directed graph.
|
43
|
+
:
|
44
|
+
: @param $rdfxml node() is the RDF/XML
|
45
|
+
: @return xs:string javascript
|
46
|
+
:)
|
47
|
+
declare function rdfxml2json:rdfxml2json
|
48
|
+
($rdfxml)
|
49
|
+
as xs:string
|
50
|
+
{
|
51
|
+
let $ntriples := rdfxml2nt:rdfxml2ntriples($rdfxml)
|
52
|
+
let $lines := fn:tokenize($ntriples , "\n")[fn:normalize-space(.) ne ""]
|
53
|
+
let $uri := fn:replace( fn:substring-before($ntriples, " "), '[<>]' , '' )
|
54
|
+
let $xml :=
|
55
|
+
element xml {
|
56
|
+
for $l in $lines
|
57
|
+
order by $l
|
58
|
+
return
|
59
|
+
element spo {
|
60
|
+
element s {
|
61
|
+
let $s := fn:substring-before($l, " ")
|
62
|
+
return
|
63
|
+
(
|
64
|
+
attribute sclean { fn:replace($s , '[<>]' , '') },
|
65
|
+
text { $s }
|
66
|
+
)
|
67
|
+
},
|
68
|
+
element p {
|
69
|
+
let $p := fn:substring-before(fn:substring-after($l, " "), " ")
|
70
|
+
return
|
71
|
+
(
|
72
|
+
attribute pclean { fn:replace($p , '[<>]' , '') },
|
73
|
+
text { $p }
|
74
|
+
)
|
75
|
+
},
|
76
|
+
element o {
|
77
|
+
let $o := fn:replace(fn:substring-after(fn:substring-after($l, " "), " ") , " \. " , "")
|
78
|
+
return
|
79
|
+
(
|
80
|
+
attribute oclean { fn:replace($o , '[<>]' , '') },
|
81
|
+
text { $o }
|
82
|
+
)
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
let $distinctSubjects := fn:distinct-values($xml/spo/s)
|
88
|
+
let $nodes :=
|
89
|
+
for $y in $distinctSubjects
|
90
|
+
return
|
91
|
+
let $distinctPredicates := fn:distinct-values($xml/spo[s = $y]/p)
|
92
|
+
let $predicatesANDobjects :=
|
93
|
+
for $z in $distinctPredicates
|
94
|
+
return
|
95
|
+
for $x in $xml/spo[s = $y and p = $z]
|
96
|
+
let $pANDo :=
|
97
|
+
fn:concat('"' , xs:string($z) , '":
|
98
|
+
[
|
99
|
+
',
|
100
|
+
fn:string-join(
|
101
|
+
for $o in $x/o
|
102
|
+
return rdfxml2json:compute-value-block(xs:string($o)),
|
103
|
+
(: return fn:concat('{ "value" : "' , xs:string($o/@oclean) , '" }'), :)
|
104
|
+
', '),
|
105
|
+
'
|
106
|
+
]')
|
107
|
+
return $pANDo
|
108
|
+
return
|
109
|
+
fn:concat('"' , xs:string($y) , '" : {
|
110
|
+
',
|
111
|
+
fn:string-join(
|
112
|
+
for $po in $predicatesANDobjects
|
113
|
+
return ($po),
|
114
|
+
',
|
115
|
+
'),
|
116
|
+
'
|
117
|
+
}')
|
118
|
+
|
119
|
+
return fn:concat(
|
120
|
+
" {
|
121
|
+
",
|
122
|
+
fn:string-join($nodes , ',
|
123
|
+
|
124
|
+
'),
|
125
|
+
"
|
126
|
+
}")
|
127
|
+
|
128
|
+
};
|
129
|
+
|
130
|
+
|
131
|
+
(:~
|
132
|
+
: This is the main function. It converts full RDF/XML to
|
133
|
+
: Protovis's Javascript for a force-directed graph.
|
134
|
+
:
|
135
|
+
: @param $rdfxml node() is the RDF/XML
|
136
|
+
: @return xs:string javascript
|
137
|
+
:)
|
138
|
+
declare function rdfxml2json:compute-value-block($o as xs:string)
|
139
|
+
{
|
140
|
+
let $o := fn:normalize-space($o)
|
141
|
+
let $firstChar := fn:substring($o, 1, 1)
|
142
|
+
|
143
|
+
let $value :=
|
144
|
+
if ($firstChar eq '"') then
|
145
|
+
if (fn:substring-before($o, '"^^')) then
|
146
|
+
fn:substring( fn:substring-before($o, '"^^') , 2)
|
147
|
+
|
148
|
+
else if (fn:substring-before($o, '"@')) then
|
149
|
+
fn:substring( fn:substring-before($o, '"@') , 2)
|
150
|
+
|
151
|
+
else
|
152
|
+
fn:substring($o, 2, ( fn:string-length($o)-2 ))
|
153
|
+
else if ($firstChar eq '<') then
|
154
|
+
fn:substring( fn:substring-before($o, '>') , 2)
|
155
|
+
else
|
156
|
+
$o
|
157
|
+
|
158
|
+
let $type :=
|
159
|
+
if ($firstChar eq "<") then
|
160
|
+
"uri"
|
161
|
+
else if ($firstChar eq "_") then
|
162
|
+
"bnode"
|
163
|
+
else
|
164
|
+
"literal"
|
165
|
+
|
166
|
+
let $dtype := fn:substring-after($o, "^^")
|
167
|
+
let $datatype :=
|
168
|
+
if ($type eq "literal" and $dtype) then
|
169
|
+
$dtype
|
170
|
+
else ()
|
171
|
+
|
172
|
+
let $lang := fn:substring-after($o, "@")
|
173
|
+
let $langvalue :=
|
174
|
+
if ($type eq "literal" and $lang) then
|
175
|
+
$lang
|
176
|
+
else ()
|
177
|
+
|
178
|
+
return
|
179
|
+
fn:concat('{
|
180
|
+
"value" : "' , $value , '",
|
181
|
+
"type" : "' , $type , '"',
|
182
|
+
if ($datatype) then
|
183
|
+
fn:concat(',
|
184
|
+
"datatype" : "' , $datatype , '"')
|
185
|
+
else "",
|
186
|
+
if ($langvalue) then
|
187
|
+
fn:concat(',
|
188
|
+
"lang" : "' , $langvalue , '"')
|
189
|
+
else "",
|
190
|
+
'
|
191
|
+
}')
|
192
|
+
|
193
|
+
};
|