Turtle - Terse RDF Triple Language
Turtle - Terse RDF Triple Language
11 September 2007
This version:
$Date: 2007/09/12 04:26:13 $ UTC
Latest version:
latest version
Previous version:
Turtle 2006-12-04
Editor:
Dave Beckett
Copyright 2003-2007
Dave Beckett
Abstract
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.
Status of this Document
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).
Table of Contents
1.
Introduction
2.
Turtle Grammar
3.
String Escapes
4.
Collections
5.
Examples
6.
URI Resolution
7.
Identifers for the Turtle Language
8.
Conformance
9.
MIME Type and Content Encoding
10.
XML QNames
11.
Possible Extensions
12.
Turtle compared to N-Triples
13.
Turtle compared to Notation3
14.
Turtle compared to SPARQL
15.
References
16.
Implementations
17.
Changes
1. Introduction
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.
2. Turtle Grammar
This EBNF is that used in XML 1.0 (Third Edition)
[NOTATION
over an alphabet of
[UNICODE]
characters encoded in UTF-8.
Turtle - Terse RDF Triple Language EBNF
[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
object
[9]
verb
::=
predicate
| 'a'
where 'a' is equivalent to the uriref
[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
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
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
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
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
object
(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
object
s 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
3. String Escapes
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
4. Collections
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
5. Examples
This example is a Turtle translation of
example 7
in the
RDF/XML Syntax specification
example1.ttl
):
@prefix rdf:
@prefix dc:
@prefix ex:
dc:title "RDF/XML Syntax Specification (Revised)" ;
ex:editor [
ex:fullname "Dave Beckett";
ex:homePage
] .
An example of an RDF collection of two literals.
@prefix :
:a :b ( "apple" "banana" ) .
which is short for (
example2.ttl
):
@prefix :
@prefix rdf:
: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 :
:a :b "The first line\nThe second line\n more" .
:a :b """The first line
The second line
more""" .
6. URI Resolution
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
# In-scope base URI is http://www.w3.org/2001/sw/DataAccess/df1/tests/ at this point
@base
# In-scope base URI is http://example.org/ns/ at this point
@base
# In-scope base URI is http://example.org/ns/foo/ at this point
@prefix :
:a4 :b4 :c4 .
@prefix :
:a5 :b5 :c5 .
encodes the following N-Triples
test-30.out
):
7. Identifers for the Turtle Language
The URI that identifies the Turtle language is:
The XML (Namespace name, Local name) pair that identifies
the Turtle language is:
Namespace:
Local name:
turtle
The suggested namespace prefix is
ttl
(informative)
which would make this
ttl:turtle
as an XML QName.
8. Conformance
Systems conforming to Turtle MUST pass all the following test cases:
The
N-Triples tests
in the
RDF Test Cases
W3C Recommendation.
The
Turtle Testsuite
tests.zip
md5sum 87997ad38ff8af359e8d2fe4af68fe81 )
These are also available as part of the
Raptor
distribution and
in Subversion in the
Raptor test cases for Turtle
Passing these tests means:
All the
test-n.ttl
tests MUST generate equivalent RDF
triples to those given in the corresponding
test-n.out
N-Triples file.
All the
bad-n.ttl
tests MUST NOT generate RDF triples.
9. MIME Type and Content Encoding
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
10. XML QNames
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.
11. Possible Extensions
12. Turtle compared to N-Triples
Turtle adds the following syntax to N-Triples:
Whitespace restrictions removed
Text content-encoding changed from ASCII to UTF-8
@prefix
QNames
[]
()
Decimal integer literals (
xsd:integer
Decimal double literals (
xsd:double
Decimal arbitrary length literals (
xsd:decimal
Boolean literals
@base
13. Turtle compared to Notation 3
(Informational)
Notation 3 includes at least the following syntax that is not in Turtle
(not a complete list):
...
is
of
paths like
:a.:b.:c
and
:a^:b^:c
@keywords
=>
implies
equivalence
@forAll
@forSome
<=
Syntax in Turtle that is not in Notation 3:
Decimal arbitrary length literals
Only double literals are currently implemented in cwm
Boolean literals
14. Turtle compared to SPARQL
(Informational)
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):
Blank nodes are allowed in triple predicates either given explicitly
with a blank node identifier using
_:
name
or with no name using
[]
or
...
RDF Literals are allowed in triple subjects
Variables are allowed in any part of the triple of the form
name
or
name
Long literals can use use single quote (
) characters:
'''
...
'''
The constants allowed for XSD booleans:
true
and
false
are case independent. In Turtle they are not.
SPARQL allows '.'s in names in all positions apart from the first or last. These would correspond to rules:
name ::= nameStartChar ( ( nameChar | '.' )* nameChar )?
prefixName ::= ( nameStartChar - '_' ) ( ( nameChar | ' .' )* nameChar )?
For further information see the
Syntax for IRIs
and
SPARQL Grammar
sections of the SPARQL query document
[SPARQLQ]
15. References
Normative
[NOTATION]
Notation
section in
Extensible Markup Language (XML) 1.0 (Third Edition)
, T. Bray, J. Paoli, C.m. Sperberg-McQueen, E. Maler, F. Yergeau editors, W3C Recommendation, 04 February 2004. This version of XML 1.0 is http://www.w3.org/TR/2004/REC-xml-20040204/. The
latest version of the Extensible Markup Language (XML) 1.0
is at http://www.w3.org/TR/REC-xml/.
[N-TRIPLES]
N-Triples
section in
RDF Test Cases
, J. Grant and D. Beckett, Editors, W3C Recommendation, 10 February 2004. This version of the RDF Test Cases is http://www.w3.org/TR/2004/REC-rdf-testcases-20040210/. The
latest version of the RDF Test Cases
is at http://www.w3.org/TR/rdf-testcases/.
[UNICODE]
The Unicode Standard Version 3.0
, Addison Wesley, Reading MA, 2000, ISBN: 0-201-61633-5. This document is http://www.unicode.org/unicode/standard/standard.html.
[CHARMOD]
Character Model for the World Wide Web 1.0: Fundamentals
, M. J. Dürst, F. Yergeau, R. Ishida, M. Wolf, T. Texin editors, W3C Recommendation, 15 February 2005. This version of Character Model for the WWW 1.0: Fundamentals is http://www.w3.org/TR/2005/REC-charmod-20050215/ The
latest version
of Character Model for the WWW: Fundamentals 1.0 is at http://www.w3.org/TR/charmod/.
Informational
[NOTATION3]
Notation 3
, Tim Berners-Lee, World Wide Web Consortium
[MSWM]
Modernising Semantic Web Markup
, Dave Beckett and
presentation
given at
XML Europe 2004
, Amsterdam, 20 April 2004
[SPARQLQ]
SPARQL Query Language for RDF
, E. Prud'hommeaux, A. Seaborne, Editors. World Wide Web Consortium. W3C Candidate Recommendation, 14 June 2007. This version is http://www.w3.org/TR/2007/CR-rdf-sparql-query-20070614/. The
latest version of SPARQL Query Language for RDF
is available at http://www.w3.org/TR/rdf-sparql-query/.
16. Implementations
Raptor C RDF Parser Toolkit
by Dave Beckett (the author) has provided Turtle support since version 1.2.0.
Redland
uses Raptor to provide language bindings to it in C#, Java, Perl, PHP, Python, Tcl and Ruby. 2003-12-31
URIQA Semantic Web Service
from Patrick Stickler, Nokia provides Turtle output. 2004-02-25
Purple RDF API for Python
by Andrea Pletrin based
Pyrple
by Sean B Palmer contains a Turtle parser. 2004-03-07.
TriG Syntax
by Chris Bizer, Freie Universität Berlin extends Turtle to serialise
Named Graphs
as described in
Named Graphs, Provenance and Trust
PDF
) by J. J. Carroll, C. Bizer, P. Hayes and P. Stickler, Proceedings of WWW2005, May 2005, Japan. See also
RDF Triples in XML (TRiX)
by Jeremy Carroll and Patrick Stickler. 2004-04-03
RIO RDF Parser
part of the
Sesame project
from Aduna
announced RIO 1.0
with Turtle support. Written in Java. 2004-10-01.
SWOOP - Hypermedia-based Featherweight OWL Ontology Editor
from
mindswap group
, University of Maryland
2.2 beta
onwards provides a Turtle syntax presentation view of the Ontology. Written in Java. 2004-10-09
SWI-Prolog
from Jan Wielemaker, Universiteit van Amsterdam
announced SWI-Prolog 5.4.3
with Turtle parsing support, 2004-10-18.
17. Changes
Changes since first publication of this document.
Added
@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.
Added
6. URI Resolution
section
to explain base URIs with an example.
Re-order sections after adding Base URIs and Indentifiers section
Update SPARQL to point to 2007-06-14 Candidate REC
2007-09-11
Added
7. Identifers for the Turtle Language
including identifiers in the form of a URI and an XML Qname.
2006-12-04
Moved
comment
to
ws
production so comments are not just at the end of statements
2006-04-03
Add rule numbers before BNF rules to allow
bnf2turtle.py
to work
2006-02-09
Fix some bugs made yesterday:
Allow an optional sign before
integer
Edit
scharacter
to remove #x9, returning longString to the 2005 definition
2006-01-02
Updated references to SPARQL WD 2005-11-23 including syntax changes:
Added
decimal
xsd:decimal
after SPARQL.
Renamed
floating
to
double
and allow optional
or
sign at the start
Updated the testcases
tests.zip
to add tests 21 and 22 for decimal and doubles
Reorganised the string escapes:
Removed #x0-#x1F and \-escapes from
character
Added
echaracter
with the \-escapes of
'\t', '\n' and '\r'
ucharacter
now forbids #x3E (>) and by
using
character
now forbids #x0-#1F.
scharacter
changed to use
echaracter
, now forbids #x0-#x1F except for
#x9.
lcharacter
changed to use
echaracter
, now forbids #x0-#x1F except for
#x9, #xA and #xD. Allows \".
Updated
nameStartChar
and
nameChar
to use names similar to SPARQL
except for allowing '.'s in the middle of the names.
Removed discussion of the name changes from section
XML QNames
2006-01-01
Added
floating
xsd:double
and
boolean
xsd:boolean
true
and
false
) forms after SPARQL.
2005-08-17
Bug fix to
literal
to restore reference to
datatypeString
and
integer
2005-07-01
Major update to the grammar tokens in light of earlier work on escapes:
Token
comment
now in terms of forbidden characters
Token
literal
now uses
quotedString
instead of langString and longLangString
Token langString deleted
Token longLangString deleted
Token
datatypeString
now uses
quotedString
Token
relativeURI
deleted note. Use
ucharacter
Token
quotedString
added with either
string
or
longString
as choices
Token
longString
added with
"""
string
"""
literals over
lcharacter
Token
character
now includes the core set of escapes
Token
hex
fixed EBNF syntax
Token
ucharacter
added for characters in a
relativeURI
Token
scharacter
added for characters in a
string
Token
lcharacter
added for characters in a
longString
Section 3 retitled as
String Escapes
and reduced to discuss only
the encoded Unicode code points.
2005-06-20
Added long literals of the form
"""
string
"""
by adding token longLangString
and added explanation in the
String Escapes
section
Removed the possible extension idea of
@charset
as changing charset during processing is tricky and confusing
Removed the possible extension idea of
@language
for now.
Added a
Turtle differences from SPARQL
section
Removed dead reference to
A Brief History of RDF Serialization Formats
by O.M. Bolzer.
Updated dead reference to
Named Graphs, Provenance and Trust
with WWW2005 paper version
Updated XML notation reference to XML 1.0 (Third Edition)
Add as a possible extension
true
and
false
constants for XSD booleans
Alter titles and update wording of of sections 10-12 comparing Turtle
with N-Triples Notation 3 and SPARQL.
Added token
hex
for a hexdecimal character
Substantially updated
String Escapes
section
to list all the allowed escapes in a new table and explain how strings
are terminated in the presence of escapes and long literals.
Explain how escapes work inside URI strings
Added reference to
Character Model for the World Wide Web 1.0: Fundamentals
(2005-06-17)
Updated the test cases to make test-13 usable as a serializing
to rdf/xml test also.
(2004-12-23)
Added a
Turtle differences from N3
section
Moved [NOTATION3] reference to informative.
(2004-12-15)
Added an
implementations section
and pointed to Raptor, SWI-Prolog, RIO parser, Purple/Pyrple, TriG and SWOOP.
Added some informational references to other alternate syntax work.
(2004-11-04)
Added a line break in the
language
production
(2004-07-29)
predicateObjectList
modified so that whitespace is optional and not required before intermediate ';'s - this makes it consistent with the 2004-07-07 change.
Added notes to
Possible Extensions
about which new features are being considered
Edited the
MIME Type
section to describe
that all Turtle content is UTF-8 and charset parameters are forbidden.
(2004-07-08)
predicateObjectList
modified so that whitespace is optional and not required before the final ';'
objectList
modified so that whitespace is optional and not required before the ','
verb
wording changed to give the uriref for the
term.
Added a
conformance
section with
an explanation of what passing means, renumbered the following sections
Added an abstract
(2004-07-07)
Added
nameStartChar
and
nameChar
and used them for defining
name
. Both now allow '_'.
Added
prefixName
using
nameStartChar
- '_')
so not to clash with
nodeID
This allows
rdf:_1
to be expressed in Turtle
and more aligns qnames with the discussion in
section
QNames
Rewrote section
QNames
to refer
to the new nameStartChar and nameChar
Deleted prefixID, merging the one use into
directive
using
prefixName
Use
resource
in
datatypeString
for the datatype URIref.
Allow an optional whitespace and ';' at the end of a
predicateObjectList
which makes certain Turtle forms easier to generate since an
objectList
can now always have a ';' following it.
Added tests for the above changes: '_' in names,
made Turtle versions of the RDF and RDFS namespace documents
(and the rdfq-results.ttl test already had a trailing ;)
in the
tests.zip
Added a table of contents, fixed duplicate section numbering
Added a status section pointing to public-cwm-talk
(2004-06-12)
Changed root term to be
turtleDoc
(2004-05-12)
Added a zip file of the testcases
tests.zip
(2004-05-12)
Allow optional whitespace inside (...)
collection
(2004-05-12)
Allow '-' in QNames
This is used in enough schemas to make it useful to add
and although the Notation3 grammars now forbid it, it is
accepted by most of the implementations. (2004-05-11)
Updated references to point to the published
XML 1.1
and
Namespaces in XML 1.1
Recommendations (2004-05-11)
Link to N-Triples tests cases (2004-03-25)
integer
literals.
OWL allows the use of
xsd:integer
(URI http://www.w3.org/2001/XMLSchema#integer)
for cardinality constraints in OWL DL
in addition to also allowing
xsd:nonNegativeInteger
(see
OWL Semantics 4.2
in definition
OWL DL ontology in RDF graph form
This is rather verbose to type out in datatyped literal form and
so it was natural to add the same form used in N3 into Turtle.
This does not add signed decimal integers. (2004-03-21)
Added initial integer literals as type xsd:nonNegativeInteger (2004-03-17)
Bug fix to match N3 - collections are a list of items with no commas allowed between them. Added the
itemList
grammar term (2004-02-26)
Pointed to the raptor Turtle tests (2004-02-26)
Changes since the
N-Triples Plus
document November/December 2003
Added
collections
Mime type changed to application/x-turtle.
Copyright 2003-2007
Dave Beckett
Last Revised: $Date: 2007/09/12 04:26:13 $