Wikivoyage:Turtle RDF

From Wikivoyage
Jump to navigation Jump to search

Turtle is a plain-text RDF representation, based on NotationThree with only the RDF-ish stuff left in. Since it leaves out a lot of the markup accounting required for RDF/XML, it's a pleasant format for learning RDF and making simple RDF documents. Turtle is nicely supported by tools like Raptor and RAP (RDF API for PHP), among others.

This document gives initial instructions for creating a FOAF document using Turtle.

Lightning intro to Turtle

[edit]

A Turtle document is a collection of RDF-triples. Each triple has the format:

<subject> <relationship> <object> .

Each statement ends with a period, and each element in the triple is an URI (except the <object>, which can be a bit of text like "string" or a number like 134). For example, to say that a person is interested in FOAF, we could use the triple:

<mailto:person@example.net> <http://xmlns.com/foaf/0.1/interest> <http://www.foaf-project.org/> .

Here, the URL for the FOAF home page stands in for the idea "FOAF", and the person's email address stands for the person. We do that a lot with RDF -- letting URLs stand for "offline" or abstract concepts. We use the FOAF vocabulary to describe being interested in someone. Most of the effort of RDF is making up URLs that have an agreed-upon meaning like "This URI means 'is interested in'". If the person is also interested in Turtle, we can say:

<mailto:person@example.net> <http://xmlns.com/foaf/0.1/interest> <http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/> .

This is pretty much all you need to know to write Turtle, but there are a lot of "shortcuts" that can make your documents shorter and more readable.

Using prefixes

[edit]

Using the full URI is a kind of unreadable mess. With Turtle, we can define a namespace prefix so that we don't have to write the same long URI prefixes all the time. A prefix definition looks like this:

@prefix pref: <uri prefix> .

For example, for FOAF elements, we can use a prefix definition like this:

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

Then we can use the topics described above like this:

@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<mailto:person@example.net> foaf:interest <http://www.foaf-project.org/> .
<mailto:person@example.net> foaf:interest <http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/> .

We can also define a default namespace prefix, in cases where we use the same namespace prefix over and over. To do this, we use the same format without a substitution string:

@prefix : <uri prefix> .

Often the "main" namespace for a Turtle document is given a default namespace. If we're working on a FOAF document, for example, we might want to make the FOAF namespace the default one:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net> :interest <http://www.foaf-project.org/> .
<mailto:person@example.net> :interest <http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/> .

Commas

[edit]

When we make two or more statements with the same subject and relationship, we can make the document shorter and more readable by combining the statements and separating the objects by one or more commas. Our topic example above could be written:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net> :interest <http://www.foaf-project.org/> , <http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/> .

Note that the meaning is exactly the same; just the number of characters typed is different.

Semicolons

[edit]

If you have the same subject in more than one statement, but different relationships, you can combine them and separate the relationship + object parts with a semicolon. For example, if we wanted to say that our person's name is "Anne Example-Person", we'd write:

@prefix : <http://xmlns.com/foaf/0.1/> .
<mailto:person@example.net> :name "Anne Example-Person" .

We could combine this with our document about Anne's interests to make:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net>
  :name "Anne Example-Person" ;
  :interest <http://www.foaf-project.org/> , <http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/> .

Note that there's still a period at the end, eventually. The newline isn't particularly necessary, but it makes the document a little more readable.

Types

[edit]

There's a special relationship between a thing and a category of things, called type (http://www.w3.org/1999/02/22-rdf-syntax-ns#type). If we want to say that Anne is a person, we can write it like this:

@prefix : <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

<mailto:person@example.net> rdf:type :Person .

Because this is such a fundamental relationship, Turtle has a special keyword a, to replace the type relationship:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net> a :Person .

Our full FOAF document for Anne becomes:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net>
  a :Person ;
  :name "Anne Example-Person" ;
  :interest <http://www.foaf-project.org/> , <http://www.ilrt.bris.ac.uk/discovery/2004/01/turtle/> .

The advantage here is that you can kind of read this out loud to yourself, and it will make some semblance of sense (in English, at least).

This URI represents a person; her name is Anne Example-Person, and her interests are FOAF and Turtle.

Anonymous things

[edit]

One nice part of FOAF is that you can describe relationships between people. For example, Anne may know a person named Uther Example, who has email address <uther@example.org> . We can write this as:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net> :knows <mailto:uther@example.org> .

<mailto:uther@example.org> a :Person; :name "Uther Example" .

When you don't want to define an URL for someone, though, you can use an anonymous object in the object area. Anonymous objects are defined with square brackets "[]" around one or more relationship-object pairs, defining the object, separated by semicolons. You can define all kinds of relationships that that object is the subject of. If we don't want to bother with Uther's email address (or we don't know it), we can just write:

@prefix : <http://xmlns.com/foaf/0.1/> .

<mailto:person@example.net> :knows [ a :Person ; :name "Uther Example" ] .

This also reads pretty nicely in English: Anne knows a person named "Uther Example".

Using other schemas

[edit]

A big part of FOAF is creating and using other, extended schemas. With Turtle, we just define a prefix for the other schema, and use the elements with the given prefix. For example, if we want to use the "relationships" schema to show that not only does Anne know Uther, but she is married to him, we can write:

@prefix : <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .

<mailto:person@example.net> rel:spouseOf [ a :Person ; :name "Uther Example" ] .

You can mix elements from different schemata as long as you namespace them. Unfortunately, you kind of have to skip over the namespaces when reading Turtle FOAF docs out loud.

[edit]
  • Turtle -- definition of the format
  • Notation3 -- a predecessor of Turtle
  • N3 Primer -- a readable tutorial outlining the ideas behind Notation3 (and thus Turtle)

<rdf> <http://rdfweb.org/topic/TurtleFoafTutorial>

 a cc:Work ;
 cc:derivativeWork <> ;
 dc:creator "Evan Prodromou" ;
 dcterms:datePublished "2005-03-12"^^dc:W3GDTF .

</rdf>