RDF Serialization Formats
- There are different serialization formats for writing RDF graphs
- They are all logically equivalent and lead to same triples
- Turtle family of RDF serialization formats
- N-Triples
- Turtle
- TriG
- N-Quads
- JSON-LD
- RDFa
- RDF/XML
N-Triples
- It is a line based, plain text format to encode a RDF graph
- It is a sequence of subject, predicate, and object separated by whitespace and terminated by dot
- IRIs are written as absolute IRIs (not shortcuts) and are enclosed in angular brackets
- Literals are written in double quotes followed by datatype or language tag
- Datatype IRI is preceded by "^^"
- Language tag is preceded by "@"
- Blank nodes are expressed as _: followed by blank node label
- _:alice <http://xmlns.com/foaf/0.1/knows> _:bob .
N-Triples
- <http://example.org/bob#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
- <http://example.org/bob#me> <http://xmlns.com/foaf/0.1/knows> <http://example.org/alice#me> .
- <http://example.org/bob#me> <http://schema.org/birthDate> "1990-07-04"^^<http://www.w3.org/2001/XMLSchema#date> .
- <http://example.org/bob#me> <http://xmlns.com/foaf/0.1/topic_interest> <http://www.wikidata.org/entity/Q12418> .
- <http://www.wikidata.org/entity/Q12418> <http://purl.org/dc/terms/title> "Mona Lisa" .
- <http://www.wikidata.org/entity/Q12418> <http://purl.org/dc/terms/creator> <http://dbpedia.org/resource/Leonardo_da_Vinci> .
- <http://data.europeana.eu/item/04802/243FA8618938F4117025F17A8B813C5F9AA4D619> <http://purl.org/dc/terms/subject> <http://www.wikidata.org/entity/Q12418> .
Turtle
- Turtle is the Terse RDF Triple Language
- It is an extension of N-Triples with syntactic shortcuts
- Namespace prefix
- Predicate lists
- Object lists
Turtle
BASE <http://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX schema: <http://schema.org/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX wd: <http://www.wikidata.org/entity/>
Turtle
- Predicate lists are triples with the same subject but different predicates and objects
- Semi colon is used as a separator
<bob#me>
a foaf:Person ;
foaf:knows <alice#me> ;
schema:birthDate "1990-07-04"^^xsd:date ;
foaf:topic_interest wd:Q12418 .
Turtle
- Object lists are triples with the same subject and predicate, but different object
- Comma is used as a separator
<ex:spiderman> foaf:name "Spiderman", "Человек-паук"@ru .
Turtle
- Two types of IRIs - relative and absolute
- Absolute IRI
- <http://one.example/subject1>
- Relative IRI
BASE <http://one.example/>
<subject1> <predicate1> <object1> .
PREFIX p: <http://one.example/>
p:subject1 p:predicate1 p:object1> .
TriG
- Extension of Turtle that supports multiple graphs
GRAPH <http://example.org/bob>
{
<bob#me>
a foaf:Person ;
foaf:knows <alice#me> ;
schema:birthDate "1990-07-04"^^xsd:date ;
foaf:topic_interest wd:Q12418 .
}
TriG
<http://example.org/bob>
dcterms:publisher <http://example.org/crc> ;
dcterms:rights <http://creativecommons.org/licenses/by/3.0/> .
N-Quads
- Extends N-Triples with a fourth element to the line which is the graph IRI of the triple
<http://example.org/bob#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <http://example.org/bob> .
<http://example.org/bob#me> <http://xmlns.com/foaf/0.1/knows> <http://example.org/alice#me> <http://example.org/bob> .
<http://example.org/bob> <http://purl.org/dc/terms/publisher> <http://example.org> .
<http://example.org/bob> <http://purl.org/dc/terms/rights> <http://creativecommons.org/licenses/by/3.0/> .
JSON-LD
- Provides JSON sytanx for RDF graphs
- JSON-LD can be used to transform JSON to RDF
- JSON objects are given universal identifiers in JSON-LD
- This helps in referring to objects in other JSON documents anywhere on the Web
JSON-LD
{
"@context": "example-context.json",
"@id": "http://example.org/bob#me",
"@type": "Person",
"birthdate": "1990-07-04",
"knows": "http://example.org/alice#me",
"interest": {
"@id": "http://www.wikidata.org/entity/Q12418",
"title": "Mona Lisa",
"subject_of": "http://data.europeana.eu/item/04802/243FA8618938F4117025F17A8B813C5F9AA4D619",
"creator": "http://dbpedia.org/resource/Leonardo_da_Vinci"
}
}
RDFa
- It is used to embed RDF within HTML and XML documents
- Search engines can make use of the structured data (schema.org, Rich Snippets) to enrich search results
RDFa
<body prefix="foaf: http://xmlns.com/foaf/0.1/
schema: http://schema.org/
dcterms: http://purl.org/dc/terms/">
<div resource="http://example.org/bob#me" typeof="foaf:Person">
<p>
Bob knows <a property="foaf:knows" href="http://example.org/alice#me">Alice</a>
and was born on the <time property="schema:birthDate" datatype="xsd:date">1990-07-04</time>.
</p>
<p>
Bob is interested in <span property="foaf:topic_interest"
resource="http://www.wikidata.org/entity/Q12418">the Mona Lisa</span>.
</p>
<</div>
</body>
RDF/XML
- Provides XML syntax for RDF graphs
- There is good tool support for XML
- This used to be the only syntax that was available to serialize RDF
RDF/XML
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:schema="http://schema.org/">
<rdf:Description rdf:about="http://example.org/bob#me">
<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
<schema:birthDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">1990-07-04</schema:birthDate>
<foaf:knows rdf:resource="http://example.org/alice#me"/>
<foaf:topic_interest rdf:resource="http://www.wikidata.org/entity/Q12418"/>
</rdf:Description>
</rdf:RDF>