This document describes TriG, a syntax for serializing Named Graphs and RDF Datasets.
TriG is a plain text format for serializing Named Graphs and RDF Datasets. The TriG syntax offers a compact and readable alternative to the XML-based TriX syntax.
TriG is roughly Turtle, extended with
Two additional rules apply:
It is recommended that TriG content is stored in files with an '.trig' suffix. The mime type of TriG is application/x-trig and the content encoding is UTF-8.
Example Document 1: This document encodes three named graphs.
# TriG Example Document 1 # This document encodes three graphs.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix swp: <http://www.w3.org/2004/03/trix/swp-1/> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix ex: <http://www.example.org/vocabulary#> . @prefix : <http://www.example.org/exampleDocument#> .
:G1 { :Monica ex:name "Monica Murphy" .
:Monica ex:homepage <http://www.monicamurphy.org> .
:Monica ex:email <mailto:monica@monicamurphy.org> .
:Monica ex:hasSkill ex:Management }
:G2 { :Monica rdf:type ex:Person .
:Monica ex:hasSkill ex:Programming }
:G3 { :G1 swp:assertedBy _:w1 .
_:w1 swp:authority :Chris .
_:w1 dc:date "2003-10-02"^^xsd:date .
:G2 swp:quotedBy _:w2 .
:G3 swp:assertedBy _:w2 .
_:w2 dc:date "2003-09-03"^^xsd:date .
_:w2 swp:authority :Chris .
:Chris rdf:type ex:Person .
:Chris ex:email <mailto:chris@bizer.de> }
Example Document 2: This document encodes a single named graph using a predicate object list and a object list. The document also shows how the optional = operator and an optional "." after each Named Graph are used in order to make TriG documents compatible with the N3 syntax.
# TriG Example Document 2
@prefix ex: <http://www.example.org/vocabulary#> . @prefix : <http://www.example.org/exampleDocument#> .
:G1 = { :Monica a ex:Person ;
ex:name "Monica Murphy" ;
ex:homepage <http://www.monicamurphy.org> ;
ex:email <mailto:monica@monicamurphy.org> ;
ex:hasSkill ex:Management ,
ex:Programming . } .
Example Document 3: This document encodes an RDF dataset. It contains a default graph and two named graphs.
# TriG Example Document 3 # This document contains a default graph and two named graphs.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
# default graph
{ <http://example.org/bob> dc:publisher "Bob" .
<http://example.org/alice> dc:publisher "Alice" . } <http://example.org/bob> { _:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> . } <http://example.org/alice> { _:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@work.example.org> . }
A TriG document is defined by the TriGDoc grammar term below, which is an extension of the Turtle grammar. Parsing it results in an RDF dataset. This EBNF is the notation used in XML 1.0 second edition over an alphabet of [UNICODE] characters.
| TriGDoc | ::= | ws* (statement ws*)* |
| statement | ::= | directive ws* '.' | graph |
| graph | ::= | graphName? ws* '='? ws* '{' ws* (triples ws* ('.' ws* triples ws*)* '.'? ws*)? '}' ws* '.'? |
| graphName | ::= | resource |
All other grammar terms are defined by the Turtle grammar as of 2006/12/04. A copy of this grammar is provided below for convinience.
| directive | ::= | '@prefix' ws+ prefixName? ':' ws+ uriref |
| triples | ::= | subject ws+ predicateObjectList Provides RDF triples using the given subject and each pair from the predicateObjectList |
| predicateObjectList | ::= | verb ws+ objectList ( ws* ';' ws* verb ws+ objectList )* (ws* ';')? Provides a sequence of (verb, object) pairs for each object from the objectList |
| objectList | ::= | object (ws* ',' ws* object)* Provides a sequence of objects |
| verb | ::= | predicate | 'a' where 'a' is equivalent to the uriref <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> |
| comment | ::= | '#' ( [^#xA#xD] )* |
| subject | ::= | resource | blank |
| predicate | ::= | resource |
| object | ::= | resource | blank | literal |
| literal | ::= | quotedString ( '@' language )? | datatypeString | integer | double | decimal | boolean |
| datatypeString | ::= | quotedString '^^' resource |
| 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. |
| 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. |
| 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. |
| exponent | ::= | [eE] ('-' | '+')? [0-9]+ |
| 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. |
| 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. |
| itemList | ::= | object (ws+ object)* Provides a sequence of objects (Note there are no commas between items unlike objectList) |
| 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. |
| ws | ::= | #x9 | #xA | #xD | #x20 | comment |
| resource | ::= | uriref | qname |
| nodeID | ::= | '_:' name |
| qname | ::= | prefixName? ':' name? See section QNames |
| uriref | ::= | '<' relativeURI '>' |
| language | ::= | [a-z]+ ('-' [a-z0-9]+ )* encoding a language tag. |
| 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] |
| nameChar | ::= | nameStartChar | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040] |
| name | ::= | nameStartChar nameChar* |
| prefixName | ::= | ( nameStartChar - '_' ) nameChar* |
| relativeURI | ::= |
ucharacter* Used as a relative URI and resolved against the current base URI to give an absolute URI reference. |
| quotedString | ::= | string | longString |
| string | ::= | #x22 scharacter* #x22 |
| longString | ::= | #x22 #x22 #x22 lcharacter* #x22 #x22 #x22 |
| 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. |
| echaracter | ::= | character |
'\t' | '\n' | '\r' See String Escapes for full details. |
| hex | ::= | [#x30-#x39] | [#x41-#x46] hexadecimal digit (0-9, uppercase A-F) |
| ucharacter | ::= | ( character - #x3E ) | '\>' |
| scharacter | ::= | ( echaracter - #x22 ) | '\"' |
| lcharacter | ::= | echaracter | '\"' | #x9 | #xA | #xD |