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 text syntax for RDF called Turtle as an extension of the N-Triples ([N-TRIPLES]) test case format carefully taking the most useful and appropriate things added from Notation 3 ([NOTATION3]) while keeping the syntax describing only RDF graphs.
This section describes the status of this document at the time of its publication. Other documents may supersede this document.
This September 2007 publication is a work in progress that may change at any time.
Comments on this document should be sent to the public-cwm-talk list, although private comments are also welcome.
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).
Turtle, the Terse RDF Triple Language, 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.
This work is described in the paper New Syntaxes for RDF which discusses other RDF syntaxes and the background to Turtle (Submitted to WWW2004, referred to as N-Triples Plus there).
It has several implementations and as a mostly subset of Notation 3 (N3) (see Turtle compared to Notation 3), is 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 EBNF is that used in XML 1.0 (Third Edition) [NOTATION over an alphabet of [UNICODE] characters encoded in UTF-8.
[1] | turtleDoc | ::= | statement* |
[2] | statement | ::= | directive ws* '.' ws* | triples ws* '.' ws* | ws+ |
[3] | directive | ::= | prefixID | base |
[4] | prefixID | ::= | '@prefix' ws+ prefixName? ':' ws+ uriref |
[5] | base | ::= | '@base' ws+ uriref |
[6] | triples | ::= | subject ws+ predicateObjectList Provides RDF triples using the given subject and each pair from the predicateObjectList |
[7] | predicateObjectList | ::= | verb ws+ objectList ( ws* ';' ws* verb ws+ objectList )* (ws* ';')? Provides a sequence of (verb, object) pairs for each object from the objectList |
[8] | objectList | ::= | object (ws* ',' ws* object)* Provides a sequence of objects |
[9] | verb | ::= | predicate | 'a' where 'a' is equivalent to the uriref <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> |
[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]+ Interpreted as an xsd:integer and generates a datatyped literal with the datatype uriref http://www.w3.org/2001/XMLSchema#integer and canonical lexical representation of xsd:integer which includes allowing no leading zeros. |
[17] | double | ::= | ('-' | '+') ? ( [0-9]+ '.' [0-9]* exponent | '.' ([0-9])+ exponent | ([0-9])+ exponent ) Interpreted as an xsd:double and generates a datatyped literal with the datatype uriref http://www.w3.org/2001/XMLSchema#double and any legal lexical representation of xsd:double. |
[18] | decimal | ::= | ('-' | '+')? ( [0-9]+ '.' [0-9]* | '.' ([0-9])+ | ([0-9])+ ) Interpreted as an xsd:decimal and generates a datatyped literal with the datatype uriref http://www.w3.org/2001/XMLSchema#decimal and any legal lexical representation of xsd:decimal. |
[19] | exponent | ::= | [eE] ('-' | '+')? [0-9]+ |
[20] | boolean | ::= | 'true' | 'false' Interpreted as an xsd:boolean and generates a datatyped literal with the datatype uriref http://www.w3.org/2001/XMLSchema#boolean and canonical lexical representation of xsd:boolean. |
[21] | blank | ::= | nodeID | '[]' | '[' ws* predicateObjectList ws* ']' | collection Provides a blank node either from the given nodeID, a generated one, a generated one which is also used to provide the subject of RDF triples for each pair from the predicateObjectList or the root of the collection. |
[22] | itemList | ::= | object (ws+ object)* Provides a sequence of objects (Note there are no commas between items unlike objectList) |
[23] | collection | ::= | '(' ws* itemList? ws* ')' Provides a blank node at the start of an RDF collection of the objects in the itemList. See section Collections for the triples generated. |
[24] | ws | ::= | #x9 | #xA | #xD | #x20 | comment |
[25] | resource | ::= | uriref | qname |
[26] | nodeID | ::= | '_:' name |
[27] | qname | ::= | prefixName? ':' name? See section QNames |
[28] | uriref | ::= | '<' relativeURI '>' |
[29] | language | ::= | [a-z]+ ('-' [a-z0-9]+ )* encoding a language tag. |
[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* Used as a relative URI and resolved against the current base URI to give an absolute URI reference. |
[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] See String Escapes for full details. |
[39] | echaracter | ::= | character |
'\t' | '\n' | '\r' See String Escapes for full details. |
[40] | hex | ::= | [#x30-#x39] | [#x41-#x46] hexadecimal digit (0-9, uppercase A-F) |
[41] | ucharacter | ::= | ( character - #x3E ) | '\>' |
[42] | scharacter | ::= | ( echaracter - #x22 ) | '\"' |
[43] | lcharacter | ::= | echaracter | '\"' | #x9 | #xA | #xD |
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 |
The triples that are generated is given by these expansions to the longer 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
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""" .
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
and 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 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 is application/x-turtle (if registered, application/turtle will be sought). Charset parameters on the mime type are forbidden, the content encoding of Turtle content is always UTF-8.
The file suffix is .ttl
The qname definition here is not the same as either XML but is designed to align with the style of abbreviated names used in SPARQL [SPARQLQ] with some differences as SPARQL allows a '.' in names in positions after the first and before the last character.
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.
For further information see the Syntax for IRIs and SPARQL Grammar sections of the SPARQL query document [SPARQLQ].
Changes since first publication of this document.
@base
directive
to set the base URI in the same fashion as
xml:base
and BASE
in SPARQL.
Notation 3 has also added this directive. +
or -
sign at the starttrue
and false
) forms after SPARQL."""
string"""
literals over lcharacter"""
string"""
by adding token longLangString
and added explanation in the String Escapes section@charset
as changing charset during processing is tricky and confusing@language
for now.true
and
false
constants for XSD booleansa
term.rdf:_1
to be expressed in Turtle
and more aligns qnames with the discussion in
section QNamesChanges since the N-Triples Plus document November/December 2003
Copyright 2003-2007 Dave Beckett
Last Revised: $Date: 2007/09/12 04:26:13 $