RDF Serialization
Prof. Dr. Sören Auer Seite 1
What are URIs?
- URI = Uniform Resource Identifier.
- Used for worldwide, unique identification of resources.
- Every object (in the context of the application) maybe a resource.
● As long as it has a unique identity.
● E.g. books, places, people, relation between those things, abstract concepts.
- Unique Identifiers were already used for other and more specific domains, e.g. ISBN for books or tax
identification numbers for people.
- Extension of the URL concept:
● Not every URI belongs to a webpage, but often a URL is used as a URI for web pages.
3.1.1 What are URIs?
Prof. Sören Auer Page 2
Syntax of URIs
- Tim Berners-Lee submitted 1994 the RFC (request for comments) 1630 about URIs.
● [Link] (current version: RFC 3986 of 2009)
● Protocol (e.g. http, ftp, mailto) and hierarchy separated by ':'
■ Queries parameters can be appended using a leading '?'
■ Fragment identifiers can be appended using a leading '#'
protocol ":" hierarchy [ "?" query ] [ "#" fragment]
3.1.2 Syntax of URIs
Prof. Sören Auer Page 3
Self- defined URIs
- Needed if a resource has no URI yet.
- Possible strategy to avoid overlapping URIs.
● Use HTTP URIs of your own webspace!
● It is also possible to publish documentation of the URI at this place.
● E.g. [Link] (a person, not a document)
[Link] Self- defined URIs
Prof. Sören Auer Page 4
Other Identification Systems
- IRI = Internationalized Resource Identifier
● Generalization of URI, can contain Unicode characters.
● E.g. [Link]
- URN = Uniform Resource Name
● Subset of URIs, used for identifying resource with freely choosable names.
● Intended for worldwide unique and persistent identification.
● E.g. urn:issn:0167-6423 URN of a Spider Man movie
- ISBN = International Standard Book
● Number E.g. ISBN 978-3-86680-192-9
3.2.1 Other Identification Systems
Prof. Sören Auer Page 5
Other Identification Systems
- ISSN = International Standard Serial Number
● E.g. ISSN 1234-5678
- DOI = Digital Object Identifier
● E.g. DOI 10.1000/182
Prof. Sören Auer 3.2.2 Other Identification Systems Page 6
Expressiveness of
RDF formats
3.3 Expressiveness of RDF formats
Prof. Sören Auer Page 7
Most popular formats
- Various serialization formats for different purposes (as on the Venn diagram on the slide before):
● N-Triples – a text format focusing on simple parsing.
● Turtle – a text format focusing on human readability.
● Notation 3 (N3) – a text format with advanced features beyond RDF.
● RDF/XML – the official XML serialization of RDF.
● JSON-LD – the official JSON serialization of RDF (supersedes earlier alternative approaches,
e.g. RDF/JSON).
● RDFa – a mechanism for embedding RDFa in (X)HTML.
Prof. Sören Auer 3.4 Most popular formats Page 8
Notation 3 (N3)
- Designed for human readability.
- Formal language
● additional formulas, rules and variables
- developed by Tim Berners-Lee et al.
[Link]
- Has internet media type or MIME (multipurpose internet mail extension) type text/n3, and is
encoded in UTF-8
3.5.1 Notation 3 (N3)
Prof. Sören Auer Page 9
Notation 3 (N3)
XML Notation
<rdf:RDF xmlns:rdf="[Link]
xmlns:dc="[Link]
<rdf:Description rdf:about="[Link]
<dc:title>Tony Benn</dc:title>
<dc:publisher>Wikipedia</dc:publisher>
</rdf:Description>
</rdf:RDF>
Notation 3:
@prefix dc: <[Link]
<[Link] dc:title "Tony Benn";
dc:publisher "Wikipedia".
Prof. Sören Auer 3.5.2 Notation 3 (N3) Page 10
Aims of N3
- Optimize expression of data and logic in the same language.
- Allow RDF to be expressed.
- Allow rules to be integrated smoothly with RDF.
- Allow quoting so that statements about statements can be made.
- Be as readable, natural, and symmetrical as possible.
Prof. Sören Auer 3.5.3 Aims of N3 Page 11
Features of N3
- URI abbreviation using prefixes which are bound to a namespace (using @prefix) a bit like in
XML.
- Repetition of another object for the same subject and predicate using a comma ","
- Repetition of another predicate for the same subject using a semicolon ";"
- Bnode syntax with a certain properties just put the properties between [ and ]
- Formulae allowing N3 graphs to be quoted within N3 graphs using { and }
- Variables and quantification to allow rules, etc to be expressed.
- A simple and consistent grammar.
Prof. Sören Auer [Link] Features of N3 Page 12
Features of N3
- N3 is a formal language that goes beyond RDF.
- N3 is a superset of SPARQL graph patterns, Turtle and N-Triples.
- N3 is more powerful than Turtle and RDF/XML with respect to its formalism.
- N3 is based on a context free grammar allowing to parse it easily.
Prof. Sören Auer [Link] Features of N3 Page 13
Syntax of N3
- Triple format: Subject Predicate Object.
- Everything has to be identified by a URI
● including relative URIs such as <#fragment>
● which are relative to the base URI
● which is the default of the document's URL.
■ Exception: _:node is a blank node ID
■ Exception: Object can be a literal
<#pat> <#knowsAbout> <[Link]
<#pat> <#hasBrother> _:ian.
_:ian. <#age> 24.
Prof. Sören Auer 3.5.5 Syntax of N3 Page 14
Abbreviations
- URIs relative to the base URI can be empty:
<> <[Link] “RDF Serializations”.
- One can use prefixes to shorten the text
@prefix dc: <http:[Link]/dc/elements/1.1/> .
<> dc:title "RDF Serializations" .
NOTE: when you use a prefix, you use a colon instead of a hash/slash between dc and title, and you don't use the <angle
brackets>
Prof. Sören Auer [Link] Abbreviations Page 15
Abbreviations
- If you have several statements about the same subject you can use either a semicolon ; to
introduce new predicates or a comma to introduce new objects
<> <#subsection> <#RDF/XML>,
<#JSON>,
<#RDFa>;
<#madeBy> "[Link]";
<#creatorOfThisDeck> <[Link]
[Link] Abbreviations
Prof. Sören Auer Page 16
Defining types
- You can define your own classes on demand, within the data:
@prefix : <#> .
@prefix rdf: <[Link] .
@prefix rdfs: <[Link] .
:Person rdf:type rdfs:Class .
Note: we defined an empty prefix.
- We even can abbreviate rdf:type with a
@prefix : <#> .
@prefix rdfs: <[Link] .
:Person a rdfs:Class .
3.5.7 Defining types
Prof. Sören Auer Page 17
Equivalence of vocabularies
- When writing own vocabularies one often notices that an own concept is the same as in another
vocabulary.
- N3 has a special and short mechanism to align vocabularies, i.e. "=" (shorthand for owl:sameAs)
:Woman = foo:FemaleAdult .
:Title a rdf:Property; = dc:title .
- So we can align classes and properties pretty easily
3.5.8 Equivalence of vocabularies
Prof. Sören Auer Page 18
Examples of Features
@prefix log: <[Link]
@keywords.
@forAll x, y, z. {
x hasParent y.
y hasSister z
}
log:implies {
x hasAunt z
}
Given the following data:
Joe hasParent Alan.
Alan hasSister Susie.
A reasoner could conclude:
Joe hasAunt Susie.
[Link] Examples of Features
Prof. Sören Auer Page 19
Turtle Syntax
- Turtle – Terse RDF Triple Language (subset of N3)
- URIs in angle brackets: <[Link]
- Literals in quotes:
● "Berlin"@de
● "51.333332"^^xsd:float
- A triple is terminated by a dot:
<[Link] <[Link] "Leipzig"@de .
- White spaces and line breaks are ignored outside of identifiers.
- Status:W3C Recommendation 25 February 2014 ,[Link]
3.6.1 Turtle Syntax
Prof. Sören Auer Page 20
Turtle Abbreviations
- In Turtle one can use abbreviations
● Syntax: @prefix abbr ':' <URI> .
● E.g. @prefix dbr: <[Link] .
- One can transform
<[Link] <[Link] "Leipzig"@de.
- Into
@prefix dbr: <[Link] .
@prefix rdfs: <[Link] .
dbr:Leipzig rdfs:label "Leipzig"@de .
Prof. Sören Auer 3.6.2 Turtle Abbreviations (1/2) Page 21
Turtle Abbreviations
- Triples with the same subject can be grouped together.
@prefix rd:
...
@prefix geo:
dbr:Berlin dbpedia:country dbpedia: Germany ;
rdfs:label "Berlin"@de;
- Even triples with the same subject and predicate can be grouped together.
@prefix rd:
...
@prefix dbr:
Leipzig dbp:locatedin dbr:Saxony,
dbr: Germany;
dbp:hasMayor dbr:Burkhard_Jung .
3.6.3 Turtle Abbreviations (1/2)
Prof. Sören Auer Page 22
Advantages and Disadvantages of Turtle
- Advantages:
● Concise, thus efficient to store
● Easy to read for humans
- Disadvantages:
● Limited tool support so far (compared to RDF/XML)
Prof. Sören Auer 3.6.4 Advantages and Disadvantages of Turtle Page 23
Semantic Annotation in the Web
- In principle there are three ways to embed structured data with explicit semantic annotations within HTML
documents:
● Domain specific microformats.
● Generic RDFa.
● HTML5 Microdata (including [Link])
3.7.2 Semantic Annotation in the Web
Prof. Sören Auer Page 24
Microformats
- Microformats emerged about 2005.
- (X)HTML Markup to express (limited) semantics in an HTML document.
● Designed to solve simple, specific problems.
● Designed for humans first, machines second.
● Used in web pages to describe a specific type of information, as e.g., a person, an event, a product, a
review, etc.
- Applications can easily extract data from HTML documents.
- In general, Microformats use the class attribute in HTML tags (most times <span> or <div> tags) and assign
brief and descriptive names to entities and their properties
Prof. Sören Auer 3.8.1 Microformats Page 25
Microformats
- Simple Example: HTML marked up with hCard microformat
<div class=”vcard”>
<span class=”fn”>Joe Blow</span>
<span class=”title”>Senior Manager</span>
<span class=”org”>The Example Company</span>
<span class=”adr”>
<span class=”street-address”>Haptstr. 123, </span>
<span class=”postal-code”>14482</span>
</span>
Tel. <span class=”tel”>604-555-1234</span>
<img class=”photo” src=”[Link]/[Link]” />
</div>
Prof. Sören Auer 3.8.2 Microformats Page 26
Microformats - Pros and Cons
- Microformats can be easily trancoded to RDF via XSLT.
- New microformat vocabularies first must be consolidated by the community, while always a new
XSLT stylesheet has to be developed for extraction.
- By using more than one microformat vocabulary in a single (X)HTML document the processing
complexity increases rapidly.
- Conflicts with used (X)HTML attributes might be possible
3.8.3 Microformats - Pros and Cons
Prof. Sören Auer Page 27
Motivation
On the left side, is what the browser sees. On the right side, is what the humans see. The crucial
question here is, can we bridge the gap so that the browsers see more of what the humans see?
3.9 Motivation
Prof. Sören Auer Page 28
RDFa Syntax
- RDFa = RDF in HTML attributes.
- Developed to embed RDF into HTML and XML.
- Embedded triples can be accessed or extracted.
- IRIs can be used (XML and HTML nowadays typically encoded as UTF-8 Unicode).
- Enables generic RDF annotations in (X)HTML documents by reusing existing (X)HTML attributes.
- RDFa 1.0 based on XHTML (W3C Recommendation 2008).
- RDFa 1.1 based on HTML5 (W3C Recommendation June 2012).
● RDFa Lite 1.1
● RDFa 1.1 3.10.1 RDFa Syntax
Prof. Sören Auer Page 29
RDFa Lite 1.1
- RDFa reuses existing (X)HTML attributes (e.g. href, src) and introduces new HTML
attributes
● vocab
● typeof
● property
● resource
● prefix
3.10.2 RDFa Lite 1.1
Prof. Sören Auer Page 30
RDFa Lite 1.1
- First we need a vocabulary to explain things
<p vocab=”[Link]
My names is Jane Doe and you can
give me a ring via 1-800-555-0527
</p>
3.10.3 RDFa Lite 1.1
Prof. Sören Auer Page 31
RDFa Lite 1.1
- The vocabulary could be more definite
<p vocab=”[Link] typeof=”Person”>
My names is Jane Doe and you can
give me a ring via 1-800-555-0527
</p>
3.10.4 RDFa Lite 1.1
Prof. Sören Auer Page 32
RDFa Lite 1.1
- The work could be more machine actionable like this
<p vocab=”[Link] typeof=”Person”>
My names is
<span property=”name”>Jane Doe</span>
and you can give me a ring via
<span property=”phone”>1-800-555-0527</span>
</p>
Prof. Sören Auer 3.10.5 RDFa Lite 1.1 Page 33
RDFa Lite 1.1
- The work could be more machine actionable like this
<p vocab=”[Link]
resource=”@Jane” typeof=”Person”>
My names is
<span property=”name”>Jane Doe</span>
and you can give me a ring via
<span property=”phone”>1-800-555-0527</span>
</p>
- Resource refers to the base URI of the web page
3.10.6 RDFa Lite 1.1
Prof. Sören Auer Page 34
RDFa Lite 1.1
- And if the vocabulary is not sufficient to describe all properties, we can use additional
vocabularies by using prefixes
<p vocab=”[Link]
prefix=”ov: [Link]
resource=”@Jane” typeof=”Person”>
My names is
<span property=”name”>Jane Doe</span>
and you can give me a ring via
<span property=”phone”>1-800-555-0527</span>.
My favorite beverage is
<span property=”ov:preferredBeverage”>coffee</span>
</p>
Prof. Sören Auer 3.10.7 RDFa Lite 1.1 Page 35
RDFa Attributes
- Some of the following attributes are reused from HTML. In particular, href and src are redundant with
resource but convenient in HTML:
● about: a CURIE or IRI, used for stating what the data is about (a subject in RDF terminology)
● content: a CDATA string, for supplying machine-readable content for a literal (a literal object, in
RDF terminology).
● datatype: a term or CURIE or absolute IRI representing a data type, to express the datatype of
a literal.
● href: a traditionally navigable IRI for expressing the partner resource of a relationship (a
resource object, in RDF terminology).
● inlist: An attribute used to indicate that the object associated with a rel or property attribute on
the same element is to be added to the list for that predicate. The value of this attribute must be
ignored. Presence of this attribute causes a list to be created if it does not already exist.
3.10.8 RDFa Attributes
Prof. Sören Auer Page 36
RDFa Attributes
● prefix: a white space separated list of prefix-name IRI pairs of the form NCName ':' ' '+
xsd:anyURI
● property: a white space separated list of terms or CURIEs or absolute IRIs, used for
expressing relationships between a subject and either a resource object if given or some
literal text (also a predicate).
● rel: a white space separated list of terms or CURIEs or absolute IRIs, used for expressing
relationships between two resources (predicates in RDF terminology).
● resource: a CURIE or IRI for expressing the partner resource of a relationship that is not
intended to be navigable (e.g., a “clickable” link) (also an object).
● rev: a white space separated list of terms or CURIEs or absolute IRIs, used for
expressing reverse relationships between two resources (also predicates).
3.10.9 RDFa Attributes
Prof. Sören Auer Page 37
RDFa Attributes
● src: an IRI for expressing the partner resource of a relationship when the resource is
embedded (also a resource object).
● typeof: a white space separated list of terms or CURIEs or absolute IRIs that indicate the
RDF type(s) to associate with a subject.
● vocab: A IRI that defines the mapping to use when a term is referenced in an attribute
value.
3.10.10 RDFa Attributes
Prof. Sören Auer Page 38
RDFa Example
<div vocab="[Link] resource="#bbg" typeof="LocalBusiness">
<h1 property="name">Beachwalk Beachwear & Giftware</h1>
<span property="description"> A superb collection of fine gifts and clothing
to accent your stay in Mexico Beach.</span>
<div property="address" resource="#bbg-address" typeof="PostalAddress">
<span property="streetAddress">3102 Highway 98</span>
<span property="addressLocality">Mexico Beach</span>,
<span property="addressRegion">FL</span>
</div>
Phone: <span property="telephone">850-648-4200</span>
</div>
3.10.10 RDFa Attributes
Prof. Sören Auer Page 39
RDFa Example (Visualized)
via: [Link]/play/
3.10.10 RDFa Attributes
Prof. Sören Auer Page 40
Advantages and Disadvantages of RDFa
- Advantages:
● Integrated handling of human (HTML) and machine (RDF) representation.
● Re-uses a number of HTML features.
● "principles of interoperable metadata" are enforced by RDFa:
- Publisher Independence: every web site can use its own representation.
- Data Reuse: data is not duplicated. Separate RDF and HTML sections are not required
anymore for the same content.
- Self Containment: RDF data is nevertheless separated from the HTML (sitting in
special attributes, can be extracted).
- Schema Modularity: attributes are reusable.
- Evolvability: additional fields can be added and XML transforms can extract the
semantics of the data from an HTML file.
3.10.11 Advantages and Disadvantages of RDFa
Prof. Sören Auer Page 41
Advantages and Disadvantages of RDFa
- Disadvantages:
● Less readability than the Turtle.
● Backwards compatibility with RDFa 1.0 adds some overhead.
3.10.12 Advantages and Disadvantages of RDFa
Prof. Sören Auer Page 42
JSON- LD
- JSON: stands for JavaScript Object Notation.
- JSON data are JavaScript data structures.
- For most other programming languages there also exist parsers
- JSON-LD: stands for JavaScript Object Notation for Linking Data.
- It is a W3C Recommendation of 2014.
- Plain JSON example:
{
"name" : "John Lennon",
"born" : "1940-10-09",
"spouse" : "[Link]
}
- How can we enrich this with semantics?
- JSON- LD answer: add an RDF context!
Prof. Sören Auer 3.11.1 JSON- LD Page 43
JSON-LD Contexts
- @context: is a special keyword to make explicit the semantic context in which some JSON data
is communicated.
- The context includes, e.g., name-to-IRI mappings.
- The @id keyword assigns IRIs to things.
Prof. Sören Auer 3.11.2 JSON-LD Contexts Page 44
JSON-LD Contexts
{
"@context":
{
"name": "[Link] ",
"born":
{
"@id": "[Link] ",
"@type": "[Link] "
},
"spouse":
{
"@id": "[Link] ",
"@type": "@id"
}
},
"@id": "[Link] ",
"name": "John Lennon",
"born": "1940-10-09",
"spouse": "[Link]
}
Prof. Sören Auer 3.11.3 JSON-LD Contexts Page 45
JSON- LD Contexts
- Note that the remaining JSON remained unchanged.
- It is possible to centralise context definitions in external locations and point to them:
{
"@context" : "[Link] ",
"@id": "[Link]
"name": "John Lennon"
….
}
Prof. Sören Auer 3.11.4 JSON- LD Contexts Page 46
JSON-LD Example
- Here is an example about a place
{
"@context": {
"name": "[Link]
"description": "[Link]
"image": {"@id": "[Link] "@type": "@id"},
"geo": "[Link]
"latitude": {"@id": "[Link] "@type": "xsd:float"},
"longitude": { "@id": "[Link] "@type": "xsd:float"},
"xsd": "[Link]
},
"name": "The Empire State Building",
"description": "The Empire State Building is a 102-story landmark in New York City.",
"image": "[Link]
"geo": {
"latitude": "40.75",
"longitude": "73.98"
}
}
Prof. Sören Auer 3.11.4 JSON- LD Contexts Page 47
JSON-LD Example (Visualized)
- The previous example visualized as nodes and edges
geo
desc
name
image
lat long
The Empire State The Empire State
Building Building is … 40.75 73.98
[Link]
Prof. Sören Auer 3.11.4 JSON- LD Contexts Page 48
RDF/JSON
- Goal of JSON-LD: add RDF semantics to any existing JSON data (having an arbitrary
structure).
- Goal of RDF/JSON: encode existing RDF graphs in a straightforward way (using a prescribed
JSON structure)
- Some notes on the history of RDF/JSON vs. JSON-LD:
[Link]
Prof. Sören Auer 3.12.1 RDF/JSON Page 49
RDF/JSON
{
"[Link] :
{
"[Link]
[{
"type": "uri" ,
"value": "[Link]
}],
"[Link]
[{
"type" : "literal" ,
"value":"Leipzig", "lang":"en"
}],
"[Link]
[{
"type" : "literal",
"value": "51.333",
"datatype": "[Link]
}],
"[Link]
[{
"type":"literal" , "value": "12.3833",
"datatype":"[Link]
}]
}
}
3.12.2 RDF/JSON
Prof. Sören Auer Page 50
RDF/JSON Syntax
- RDF/ JSON has the form subject S, predicate P, object O.
{"s" : {"p" : [O] }}
- Type: has to be an URI, literal or blank node AND has to be written in a lowercase.
- Value: Describes data of an object.
● Best practice: Render the whole URI.
- Lang: language of a literal. Optional, but if it exists it might cannot be empty.
- DataType: Data type of an object (optional).
3.12.3 RDF/JSON Syntax
Prof. Sören Auer Page 51
Advantages and Disadvantages of JSON-LD
- Advantages
● Compact data format to exchange data between applications.
● Very good tool support (almost every programming language supports JSON).
● Less overhead while parsing and serialization than XML.
- Disadvantages
● RDF structures that go beyond key/value pairs (i.e. property/object pairs attached to a
given subject).
● Not as easy to read for humans as in Turtle.
3.12.4 Advantages and Disadvantages of JSON-LD
Prof. Sören Auer Page 52
RDF/XML
- Builds on XML structure and encodes RDF semantics within tags and attributes.
- Data feels like XML:
● can be queried by XPath/Query and transformed by XSLT
● can be parsed into an RDF graph.
- Parsers exists for XML formats.
Prof. Sören Auer 3.12.1 RDF/JSON Page 53
RDF/XML
- Elements:
● <rdf:RDF> Root element
● <rdf:Description> collects a (partial) description of a resource
● @rdf:about=“uri” references the described resource
● <rdf:type> defines the class to which a resource belongs
- Full syntax:
<rdf:Description rdf:about=“uri”>
<rdf:type rdf:resource=“classname”>
resource description
<property1>value</property1>
<property2 rdf:resource=“uri”/>
</rdf:Description>
Prof. Sören Auer 3.12.1 RDF/JSON Page 54
RDF/XML Example
- An example about two records from a CD-list:
<rdf:RDF
xmlns:rdf="[Link]
xmlns:cd="[Link]
<rdf:Description rdf:about="[Link] Burlesque">
<cd:artist>Bob Dylan</cd:artist>
<cd:country>USA</cd:country>
<cd:company>Columbia</cd:company>
<cd:price>10.90</cd:price>
<cd:year>1985</cd:year>
</rdf:Description>
<rdf:Description rdf:about="[Link] your heart">
<cd:artist>Bonnie Tyler</cd:artist>
<cd:country>UK</cd:country>
….
</rdf:Description>
</rdf:RDF>
Prof. Sören Auer 3.12.1 RDF/JSON Page 55