Copyright 2003-2007 Dave Beckett
The Resource Description Framework (RDF) is a general-purpose language for representing information in the Web.
This document defines a textual syntax for RDF called Turtle that allows RDF graphs to be completely written in a compact and natural text form, with abbreviations for common usage patterns and datatypes. Turtle provides levels of compatibility with the existing N-Triples and Notation 3 foramts as well as the triple pattern syntax of the SPARQL W3C Proposed Recommendation.
This section describes the status of this document at the time of its publication. Other documents may supersede this document.
This 20 November 2007 publication is a work in progress that may change at any time.
The changes since the last version 2007-09-11 are defined in the Changelog but include a major document reorganisation, removal of whitespace explicitly from the grammar, a new introductory section explaining the syntax and removal of requirements for canonicalisation of lexical forms.
Comments on this document should be sent to the public-cwm-talk list, although private comments are also welcome.
This document defines Turtle, the Terse RDF Triple Language, a concrete syntax for RDF as defined in the RDF Concepts and Abstract Syntax ([RDF-CONCEPTS]) W3C Recommendation. Turtle is an extension of N-Triples ([N-TRIPLES]) carefully taking the most useful and appropriate things added from Notation 3 ([NOTATION3]) while keeping it in the RDF model.
The recommended XML syntax for RDF, RDF/XML ([RDF-XML]) has certain restrictions imposed by XML and the use of XML Namespaces that prevent it encoding all RDF graphs (some predicate URIs are forbidden and XML 1.0 forbids encoding some Unicode codepoints). These restrictions do not apply to Turtle.
Turtle is mostly a subset of Notation 3 (N3) (see Turtle compared to Notation 3), and is generally usable in systems that support N3.
All RDF written in Turtle should be usable inside the query
language part of the
SPARQL Protocol And RDF Query Language
(SPARQL)
[SPARQLQ]
which uses a Turtle/N3 style syntax for the Triple patterns and
for RDF triples in the CONSTRUCT
clause. This allows
using RDF written in Turtle to allow forming "queries by example",
using the data to make an initial query which can then be edited to
use variables where bindings are wanted.
This section is informative. In case of disagreement, the Turtle Grammar section is definitive.
A Turtle document allows writing down an RDF graph in a compact
textual form. It consists of a sequence of directives, triple-generating
statements or blank lines. Comments may be given after a #
and continue to the end of the line.
Simple triples are a sequence of (subject, predicate, object) terms, separated by whitespace and terminated by '.' after each triple. This corresponds to N-Triples ([N-TRIPLES]).
There are three types of RDF Term: RDF URI References (URIs for short), literals and blank nodes.
URIs are written enclosed in '<' and '>' and may be absolute RDF URI References or relative to the current base URI (described below).
# this is not a complete turtle document <http://example.org/path/> <http://example.org/path/#fragment> </path> <#fragment> <>
URIs may also be abbrevated by using Turtle's @prefix
directive that allows declaring a short prefix name for a long prefix
of repeated URIs. This is useful for many RDF vocabularies that are
all defined in nearby namespace URIs, possibly using XML's namespace
mechanism that works in a similar fashion.
Once a prefix such as @prefix foo:
<http://example.org/ns#>
is defined, any mention of a
URI later in the document may use a qualified name that
starts foo:
to stand for the longer URI. So for
example, the qualified name foo:bar
is a shorthand for
the URI http://example.org/ns#bar
.
# this is a complete turtle document @prefix foo: <http://example.org/ns#> . @prefix : <http://other.example.org/ns#> . foo:bar foo: : . :bar : foo:bar .
Literals are written either using double-quotes when they do not
contain linebreaks like "simple literal"
or
"""long literal""
when they may contain linebreaks.
# this is not a complete turtle document "a string" """a string""" """a string with newlines """
Literals may be given either a language suffix or a datatype URI
but not both. Languages are indicated by appending the simple
literal with @
and the language tag. Datatype URIs
similarly append ^^
followed by any legal URI form (full
or qualified) as described above to give the datatype URI.
# this is not a complete turtle document "chat" "chat"@en "chat"@fr "foo"^^<http://example.org/my/datatype> """10"""^^xsd:decimal
Blank nodes are written as _:
nodeID
to provide a blank node either from the given nodeID.
A generated blank node may also be made with []
which is useful to provide the subject of RDF triples for
each pair from the predicateObjectList
or the root of the collection.
# this is not a complete turtle document _:me _:a1234
Literals and URIs may also contain escapes to encode surrounding syntax, non-printable characters and to encode Unicode characters by codepoint number (although they may also be given directly, encoded as UTF-8). The character escapes are:
\t
(U+0009, tab)\n
(U+000A, linefeed)\r
(U+000D, carriage return)\"
(U+0022, double quote - only allowed inside strings\>
(U+003E, greater than - only allowed inside URIs\\
(U+005C, backslash).\u
HHHH or
\U
HHHHHHHH
for writing Unicode characters by hexadecimal codepoint where
H is a single hexadecimal digit.
See the String escapes section for full details.
The current base URI may be altered in a Turtle document using the
@base
directive. It allows further abbreviation of
URIs but is usually for simplifying the URIs in the data, where
the prefix directives are for vocabularies that describe the data.
Whenever this directive appears, it defines the base URI for which all relative URIs are resolved against. That includes URIs, qualified names, prefix directives as well as later base directives.
# this is a complete turtle document # In-scope base URI is the document URI at this point <a1> <b1> <c1> . @base <http://example.org/ns/> . # In-scope base URI is http://example.org/ns/ at this point <a2> <http://example.org/ns/b2> <c2> . @base <foo/> . # In-scope base URI is http://example.org/ns/foo/ at this point <a3> <b3> <c3> . @prefix : <bar#> . :a4 :b4 :c4 . @prefix : <http://example.org/ns2#> . :a5 :b5 :c5 .
The token a
is equivalent to the uriref
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
# this is a complete turtle document @prefix doc: <http://example.org/#ns> . <http://example.org/path> a doc:Document .
The ,
symbol may be used to repeat the subject and
predicate of triples that only differ in the object rdf term.
# this is not a complete turtle document :a :b :c , :d . # the last triple is :a :b :d .
The ;
symbol may be used to repeat the subect of
of triples that vary only in predicate and object RDF terms.
# this is not a complete turtle document :a :b :c ; :d :e . # the last triple is :a :d :e .
Decimal integers may be written directly and correspond to the XML Schema Datatype xsd:integer. in both syntax and datatype URI.
# this is not a complete turtle document -5 0 1 10 +1 # some long form examples "-5"^^xsd:integer "10"^^<http://www.w3.org/2001/XMLSchema#integer>
Decimal floating point double/fixed precision numbers may be written directly and correspond to the XML Schema Datatype xsd:double in both syntax and datatype URI.
# this is not a complete turtle document 1.3e2 10e0 -12.5e10 # some long form examples "1.3e2"^^xsd:double "-12.5e10"^^<http://www.w3.org/2001/XMLSchema#double>
Decimal floating point arbitrary precision numbers may be written directly and correspond to the XML Schema Datatype xsd:decimal. in both syntax and datatype URI.
# this is not a complete turtle document 0.0 1.0 1.234567890123456789 -5.0 # some long form examples "0.0"^^xsd:decimal "-5.0"^^<http://www.w3.org/2001/XMLSchema#decimal>
Boolean may be written directly as true
or
false
and correspond to the
the XML Schema Datatype
xsd:boolean
in both syntax and datatype URI.
# this is not a complete turtle document true false # same in long form "true"^^xsd:boolean "false"^^<http://www.w3.org/2001/XMLSchema#boolean>
An RDF Collection may be abbreviated using a sequence of
RDF Terms enclosed in ( )
brackets. Whitespace may
be used to separate them, as usual. This format provides a
blank node at the start of RDF Collection which may be used
in further abbreviations.
# this is a complete turtle document @prefix : <http://example.org/foo> . # the value of this triple is the RDF collection blank node :subject :predicate ( :a : b : c ) . # an empty collection value - rdf:nil :subject :predicate2 () .
See section Collections for the details on the long form of the generated triples.
A Turtle document is a Unicode[UNICODE] character string encoded in UTF-8. Unicode codepoints only in the range U+0 to U+10FFFF inclusive are allowed.
White space (production ws) is used to separate two tokens which would otherwise be (mis-)recognized as one token.
White space is significant in tokens relativeURI, string and longString.
Comments in Turtle take the form of '#', outside an relativeURI or strings, and continue to the end of line (marked by characters U+000D or U+000A) or end of file if there is no end of line after the comment marker. Comments are treated as white space and defined by token comment.
Turtle strings and URIs can use \
-escape sequences to
represent Unicode code points.
The following table describes all the escapes allowed inside a string, longString or relativeURI:
Escape | Unicode code point |
---|---|
'\u' hex hex hex hex | A Unicode codepoint in the range U+0 to U+FFFF inclusive corresponding to the encoded hexadecimal value. |
'\U' hex hex hex hex hex hex hex hex | A Unicode codepoint in the range U+10000 to U+10FFFF inclusive corresponding to the encoded hexadecimal value. |
'\t' | U+0009 |
'\n' | U+000A |
'\r' | U+000D |
'\"' (inside string and longString) |
U+0022 |
'\>' (inside relativeURI only) |
U+003E |
'\\' | U+005C |
URIs are resolved relative to the In-scope base URI.
The starting In-Scope Base URI is defined using
the Base URI mechanism defined in the URI RFC - dependent
on the protocol or other context outside the document.
During turtle parsing, the in-scope base URI at any point in
the document is determined by the
@base
directive
which sets a new base URI relative to the current in-scope base URI.
This directive may be repeated.
Example (test-30.ttl) with document base URI http://www.w3.org/2001/sw/DataAccess/df1/tests/
# In-scope base URI is http://www.w3.org/2001/sw/DataAccess/df1/tests/ at this point <a1> <b1> <c1> . @base <http://example.org/ns/> . # In-scope base URI is http://example.org/ns/ at this point <a2> <http://example.org/ns/b2> <c2> . @base <foo/> . # In-scope base URI is http://example.org/ns/foo/ at this point <a3> <b3> <c3> . @prefix : <bar#> . :a4 :b4 :c4 . @prefix : <http://example.org/ns2#> . :a5 :b5 :c5 .
encodes the following N-Triples (test-30.out):
<http://www.w3.org/2001/sw/DataAccess/df1/tests/a1> <http://www.w3.org/2001/sw/DataAccess/df1/tests/b1> <http://www.w3.org/2001/sw/DataAccess/df1/tests/c1> . <http://example.org/ns/a2> <http://example.org/ns/b2> <http://example.org/ns/c2> . <http://example.org/ns/foo/a3> <http://example.org/ns/foo/b3> <http://example.org/ns/foo/c3> . <http://example.org/ns/foo/bar#a4> <http://example.org/ns/foo/bar#b4> <http://example.org/ns/foo/bar#c4> . <http://example.org/ns2#a5> <http://example.org/ns2#b5> <http://example.org/ns2#c5> .
The triples that are generated by the collection term is given by these expansions to the longer triples form:
( object1 object2 ) is short for:
[ rdf:first
object1;
rdf:rest
[ rdf:first
object2;
rdf:rest
rdf:nil
] ]
( )
is short for the resource:
rdf:nil
The EBNF used here is defined in XML 1.0 (Third Edition) [NOTATION]
[1] | turtleDoc | ::= | statement* |
[2] | statement | ::= | directive '.' | triples '.' | ws+ |
[3] | directive | ::= | prefixID | base |
[4] | prefixID | ::= | '@prefix' ws+ prefixName? ':' uriref |
[5] | base | ::= | '@base' ws+ uriref |
[6] | triples | ::= | subject predicateObjectList |
[7] | predicateObjectList | ::= | verb objectList ( ';' verb objectList )* ( ';')? |
[8] | objectList | ::= | object ( ',' object)* |
[9] | verb | ::= | predicate | 'a' |
[10] | comment | ::= | '#' ( [^#xA#xD] )* |
[11] | subject | ::= | resource | blank |
[12] | predicate | ::= | resource |
[13] | object | ::= | resource | blank | literal |
[14] | literal | ::= | quotedString ( '@' language )? | datatypeString | integer | double | decimal | boolean |
[15] | datatypeString | ::= | quotedString '^^' resource |
[16] | integer | ::= | ('-' | '+') ? [0-9]+ |
[17] | double | ::= | ('-' | '+') ? ( [0-9]+ '.' [0-9]* exponent | '.' ([0-9])+ exponent | ([0-9])+ exponent ) |
[18] | decimal | ::= | ('-' | '+')? ( [0-9]+ '.' [0-9]* | '.' ([0-9])+ | ([0-9])+ ) |
[19] | exponent | ::= | [eE] ('-' | '+')? [0-9]+ |
[20] | boolean | ::= | 'true' | 'false' |
[21] | blank | ::= | nodeID | '[]' | '[' predicateObjectList ']' | collection |
[22] | itemList | ::= | object+ |
[23] | collection | ::= | '(' itemList? ')' |
[24] | ws | ::= | #x9 | #xA | #xD | #x20 | comment |
[25] | resource | ::= | uriref | qname |
[26] | nodeID | ::= | '_:' name |
[27] | qname | ::= | prefixName? ':' name? |
[28] | uriref | ::= | '<' relativeURI '>' |
[29] | language | ::= | [a-z]+ ('-' [a-z0-9]+ )* |
[30] | nameStartChar | ::= | [A-Z] | "_" | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] |
[31] | nameChar | ::= | nameStartChar | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040] |
[32] | name | ::= | nameStartChar nameChar* |
[33] | prefixName | ::= | ( nameStartChar - '_' ) nameChar* |
[34] | relativeURI | ::= | ucharacter* |
[35] | quotedString | ::= | string | longString |
[36] | string | ::= | #x22 scharacter* #x22 |
[37] | longString | ::= | #x22 #x22 #x22 lcharacter* #x22 #x22 #x22 |
[38] | character | ::= |
'\u' hex hex hex hex | '\U' hex hex hex hex hex hex hex hex | '\\' | [#x20-#x5B] | [#x5D-#x10FFFF] |
[39] | echaracter | ::= | character | '\t' | '\n' | '\r' |
[40] | hex | ::= | [#x30-#x39] | [#x41-#x46] |
[41] | ucharacter | ::= | ( character - #x3E ) | '\>' |
[42] | scharacter | ::= | ( echaracter - #x22 ) | '\"' |
[43] | lcharacter | ::= | echaracter | '\"' | #x9 | #xA | #xD |
This example is a Turtle translation of example 7 in the RDF/XML Syntax specification (example1.ttl):
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix ex: <http://example.org/stuff/1.0/> . <http://www.w3.org/TR/rdf-syntax-grammar> dc:title "RDF/XML Syntax Specification (Revised)" ; ex:editor [ ex:fullname "Dave Beckett"; ex:homePage <http://purl.org/net/dajobe/> ] .
An example of an RDF collection of two literals.
@prefix : <http://example.org/stuff/1.0/> . :a :b ( "apple" "banana" ) .
which is short for (example2.ttl):
@prefix : <http://example.org/stuff/1.0/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . :a :b [ rdf:first "apple"; rdf:rest [ rdf:first "banana"; rdf:rest rdf:nil ] ] .
An example of two identical triples containing literal objects containing newlines, written in plain and long literal forms. Assumes that line feeds in this document are #xA. (example3.ttl):
@prefix : <http://example.org/stuff/1.0/> . :a :b "The first line\nThe second line\n more" . :a :b """The first line The second line more""" .
The URI that identifies the Turtle language is:
http://www.dajobe.org/2004/01/turtle#turtle
The XML (Namespace name, Local name) pair that identifies
the Turtle language is:
Namespace: http://www.dajobe.org/2004/01/turtle#
Local name: turtle
The suggested namespace prefix is ttl
(informative)
which would make this ttl:turtle
as an XML QName.
Systems conforming to Turtle MUST pass all the following test cases:
Passing these tests means:
test-n.ttl
tests MUST generate equivalent RDF
triples to those given in the corresponding test-n.out
N-Triples file.bad-n.ttl
tests MUST NOT generate RDF triples.The mime type of Turtle if registered will be
application/turtle
(pre-registration
application/x-turtle should be used and accepted).
Charset parameters on the mime
type are forbidden, the content encoding of Turtle content is always
UTF-8.
The recommended file suffix is .ttl
Turtle adds the following syntax to N-Triples:
@prefix
,
;
[]
a
()
xsd:integer
)xsd:double
)xsd:decimal
)@base
Notation 3 includes at least the following syntax that is not in Turtle (not a complete list):
{
... }
is
of
:a.:b.:c
and :a^:b^:c
@keywords
=>
implies=
equivalence@forAll
@forSome
Syntax in Turtle that is not in Notation 3:
the SPARQL Query Language for RDF (SPARQL) [SPARQLQ] uses a Turtle/N3 style syntax for the Triple patterns including the same forms of abbreviated forms given here.
SPARQL includes at least the following syntax that is not in Turtle (not a complete list):
_:
name or with no name using []
or [
... ]
.
?
name or $
name
'
) characters:
'''
... '''
true
and
false
are case independent. In Turtle they are not,
only lowercase forms are allowed.
For further information see the Syntax for IRIs and SPARQL Grammar sections of the SPARQL query document [SPARQLQ].
This work was described in the paper New Syntaxes for RDF which discusses other RDF syntaxes and the background to the Turtle (Submitted to WWW2004, referred to as N-Triples Plus there).
This work was started during the Semantic Web Advanced Development Europe (SWAD-Europe) project funded by the EU IST-7 programme IST-2001-34732 (2002-2004) and further development supported by the Institute for Learning and Research Technology at the University of Bristol, UK (2002-Sep 2005).
Changes since the last publication of this document Turtle 2007-09-11 . See the Previous changelog for further information