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

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

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

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

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

7. Identifers for the Turtle Language

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.

8. Conformance

Systems conforming to Turtle MUST pass all the following test cases:

  1. The N-Triples tests in the RDF Test Cases W3C Recommendation.
  2. 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:

    1. All the test-n.ttl tests MUST generate equivalent RDF triples to those given in the corresponding test-n.out N-Triples file.
    2. 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:

  1. Whitespace restrictions removed
  2. Text content-encoding changed from ASCII to UTF-8
  3. @prefix
  4. QNames
  5. ,
  6. ;
  7. []
  8. a
  9. ()
  10. Decimal integer literals (xsd:integer)
  11. Decimal double literals (xsd:double)
  12. Decimal arbitrary length literals (xsd:decimal)
  13. Boolean literals
  14. @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):

  1. { ... }
  2. is of
  3. paths like :a.:b.:c and :a^:b^:c
  4. @keywords
  5. => implies
  6. = equivalence
  7. @forAll
  8. @forSome
  9. <=

Syntax in Turtle that is not in Notation 3:

  1. Decimal arbitrary length literals
    Only double literals are currently implemented in cwm
  2. 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):

  1. Blank nodes are allowed in triple predicates either given explicitly with a blank node identifier using _:name or with no name using [] or [ ... ].
  2. RDF Literals are allowed in triple subjects
  3. Variables are allowed in any part of the triple of the form ?name or $name
  4. Long literals can use use single quote (') characters: ''' ... '''
  5. The constants allowed for XSD booleans: true and false are case independent. In Turtle they are not.
  6. 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

17. Changes

Changes since first publication of this document.

Changes since the N-Triples Plus document November/December 2003


Copyright 2003-2007 Dave Beckett

Last Revised: $Date: 2007/09/12 04:26:13 $