This library contains helper functions to work with RDF triples and JSON-LD documents.
Triples are in essence three tuples: {subject, predicate, object}
.
In practice there are variations, depending on the type of the object.
We have the following two variations:
#{
<<"subject">> => <<"_:n1">>,
<<"predicate">> => <<"http://....">>,
<<"@value">> => <<"...">>,
<<"@type">> => <<"http://....">>,
<<"@language">> => <<"nl">>
}.
And, if the object is a uri:
#{
<<"subject">> => <<"_:n1">>,
<<"predicate">> => <<"http://....">>,
<<"@id">> => <<"...">>
}.
Triples can be combined into documents. A document is a map with predicates and values.
To make documents from a list of triples call:
zotonic_rdf:triples_to_docs(Triples).
This returns a list of documents, only documents with a non-blank @id
are returned.
Blank nodes are inlined in the documents.
Example document:
#{
<<"@id">> => <<"http://example.com/#a">>,
<<"http://xmlns.com/foaf/0.1/name">> => [
<<"Jan">>
],
<<"http://example.com/#foo">> => [
<<"Baz">>,
#{
<<"@value">> => <<"123">>,
<<"@type">> => <<"http://www.w3.org/2001/XMLSchema#string">>
},
#{
<<"@value">> => <<"123">>,
<<"@type">> => <<"http://www.w3.org/2001/XMLSchema#integer">>
},
#{
<<"@value">> => <<"123">>,
<<"@type">> => <<"http://example.com/#sometype">>
},
#{
<<"http://example.com/#bar">> => [
#{
<<"@value">> => <<"Bar">>,
<<"@language">> => <<"nl">>,
<<"@type">> => <<"http://www.w3.org/2001/XMLSchema#string">>
}
]
}
]
}
The documents have full uris for their types and predicates, also the predicate values are lists
and some values like integers can still be represented as strings.
The uris and values can be compacted using the zotonic_rdf:compact/1
or zotonic_rdf:compact/2
function:
zotonic_rdf:compact(Document, zotonic_rdf:namespaces()).
or, using the default namespaces in zotonic_rdf:namespaces()
:
zotonic_rdf:compact(Document).
The example document above will then become:
#{
<<"@id">> => <<"http://example.com/#a">>,
<<"foaf:name">> => <<"Jan">>,
<<"http://example.com/#foo">> => [
<<"Baz">>,
<<"123">>,
123,
#{
<<"@value">> => <<"123">>,
<<"@type">> => <<"http://example.com/#sometype">>
},
#{
<<"http://example.com/#bar">> => #{
<<"@value">> => <<"Bar">>,
<<"@language">> => <<"nl">>
}
}
]
}
The library defines a default list of namespaces for vocabularies. These are:
Use the following functions to compact or expand a namespace:
2> zotonic_rdf:ns_compact(<<"http://www.w3.org/2001/XMLSchema#string">>).
<<"xsd:string">>
3> zotonic_rdf:ns_expand(<<"xsd:string">>).
<<"http://www.w3.org/2001/XMLSchema#string">>
Optionally a map of namespaces can be passed as a second argument, this
defaults to zotonic_rdf:namespaces()
.