Skip to content

Latest commit

 

History

History
153 lines (124 loc) · 4.3 KB

README.md

File metadata and controls

153 lines (124 loc) · 4.3 KB

RDF triples handling - map triples to resources - RDF support routines.

This library contains helper functions to work with RDF triples and JSON-LD documents.

Triples

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">> => <<"...">>
}.

Documents

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">>
            }
        }
    ]
}

Namespaces

The library defines a default list of namespaces for vocabularies. These are:

aclhttp://www.w3.org/ns/auth/acl#
dbpediahttp://dbpedia.org/property/
dbpedia-owlhttp://dbpedia.org/ontology/
dchttp://purl.org/dc/elements/1.1/
dctermshttp://purl.org/dc/terms/
dctypehttp://purl.org/dc/dcmitype/
foafhttp://xmlns.com/foaf/0.1/
geohttp://www.w3.org/2003/01/geo/wgs84_pos#
rdfhttp://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfshttp://www.w3.org/2000/01/rdf-schema#
schemahttps://schema.org/
vcardhttp://www.w3.org/2006/vcard/ns#
vocabhttp://rdf.data-vocabulary.org/#
xsdhttp://www.w3.org/2001/XMLSchema#

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().