YOU CAN EDIT THIS PAGE! Just click any blue "Edit" link and start writing!

Wikitravel:Turtle RDF

From Wikitravel
Revision as of 16:42, 14 August 2010 by Wrh2 (talk | contribs) (+WikitravelDoc|help)
Jump to: navigation, 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

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:

<> <> <> .

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:

<> <> <> .

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

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: <> .

Then we can use the topics described above like this:

@prefix foaf: <> .
<> foaf:interest <> .
<> foaf:interest <> .

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 : <> .

<> :interest <> .
<> :interest <> .


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 : <> .

<> :interest <> , <> .

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


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 : <> .
<> :name "Anne Example-Person" .

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

@prefix : <> .

  :name "Anne Example-Person" ;
  :interest <> , <> .

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.


There's a special relationship between a thing and a category of things, called type ( If we want to say that Anne is a person, we can write it like this:

@prefix : <> .
@prefix rdf: <>.

<> rdf:type :Person .

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

@prefix : <> .

<> a :Person .

Our full FOAF document for Anne becomes:

@prefix : <> .

  a :Person ;
  :name "Anne Example-Person" ;
  :interest <> , <> .

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

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 <> . We can write this as:

@prefix : <> .

<> :knows <> .

<> 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 : <> .

<> :knows [ a :Person ; :name "Uther Example" ] .

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

Using other schemas

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 : <> .
@prefix rel: <> .

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

External links

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