Copyright © 2008-2023 World Wide Web Consortium. W3C® liability, trademark and permissive document license rules apply.
N-Triples is a line-based, plain text format for encoding an RDF graph.
RDF 1.2 N-Triples introduces quoted triples as a fourth kind of RDF term which can be used as the subject or object of another triple, making it possible to make statements about other statements.
This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.
This document is part of the RDF 1.2 document suite. The N-Triples format is a line-based RDF syntax based on a subset of Turtle [RDF12-TURTLE].
This document was published by the RDF-star Working Group as a Working Draft using the Recommendation track.
Publication as a Working Draft does not imply endorsement by W3C and its Members.
This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 12 June 2023 W3C Process Document.
This section is non-normative.
This document defines N-Triples, a concrete syntax for RDF [RDF12-CONCEPTS]. N-Triples is an easy to parse line-based subset of Turtle [RDF12-TURTLE].
The syntax is a revised version of N-Triples as originally defined in the RDF Test Cases [RDF-TESTCASES] document. Its original intent was for writing test cases, but it has proven to be popular as an exchange format for RDF data.
An N-Triples document contains no parsing directives.
N-Triples triples are a sequence of RDF terms representing the
subject,
predicate, and
object
of an RDF Triple.
These may be separated by white space (spaces U+0020
or tabs U+0009
).
This sequence is terminated by a '.
'
(optionally followed by white space and/or a comment),
and a new line (optional at the end of a document).
<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> . # comments here
# or on a line by themselves
_:subject1 <http://an.example/predicate1> "object1" .
_:subject2 <http://an.example/predicate2> "object2" .
N-Triples triples are also Turtle simple triples, but Turtle includes other representations of RDF terms and abbreviations of RDF Triples. When parsed by a Turtle parser, data in the N-Triples format will produce exactly the same triples as a parser for the N-triples language.
The RDF graph represented by an N-Triples document contains
exactly each triple matching the N-Triples
triple
production.
This section is non-normative.
An N-Triples document allows writing down an
RDF graph
in a textual form.
An RDF graph is made up of simple triples
consisting of a
subject,
predicate, and
object
and optional blank lines.
Comments may be given after a '#
' that is not part of
another lexical token and continue to the end of the line.
The simplest triple statement is a sequence of
(subject,
predicate, and
object) terms,
and terminated by '.
'.
White space (spaces U+0020
or tabs U+0009
) may surround terms,
except where significant as noted in the grammar.
Comments are treated as white space, and may be given after a '#
' that is not part of
another lexical token and continue to the end of the line.
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .
A quoted triple may be the subject or object of an RDF triple.
A quoted triple
is represented as a quotedTriple
with
subject
,
predicate
, and
object
preceded by two concatenated <
characters, each having the code point U+003C, and
followed by two concatenated >
characters, each having the code point U+003E.
Note that quoted triples
may be nested.
_:e38 <ex:familyName> "Smith" .
<< _:e38 <http://example.com/jobTitle> "Designer" >> <http://example.com/accordingTo> _:e22 .
IRIs may be written only as resolved IRIs.
IRIs are preceded by <
(code point U+003C) and
followed by >
(code point U+003E),
and may contain numeric escape sequences (described below).
For example <http://example.org/#green-goblin>
.
Literals are used to identify values such as strings, numbers, dates.
Literals (Grammar production Literal) have a lexical form followed by a language tag, a datatype IRI, or neither.
The representation of the lexical form consists of an
initial delimiter "
(U+0022),
a sequence of permitted characters or numeric escape sequence or string escape sequence,
and a final delimiter.
Literals may not contain the characters "
,
LF
(U+000A), or
CR
(U+000D)
except in their escaped forms.
In addition '\
' (U+005C)
may not appear in any quoted literal except as part of an escape sequence
and a "
(U+0022) character
can only be included in a quote literal using an escape sequence.
The corresponding RDF lexical form
is the characters between the delimiters, after processing any escape sequences.
If present, the language tag
is preceded by a '@
' (U+0040).
If there is no language tag, there may be a datatype IRI,
preceded by two concatenated ^
characters,
each having the code point U+005E.
If there is no datatype IRI and no language tag
it is a simple literal
and the datatype is http://www.w3.org/2001/XMLSchema#string
.
<http://example.org/show/218> <http://www.w3.org/2000/01/rdf-schema#label> "That Seventies Show"^^<http://www.w3.org/2001/XMLSchema#string> . # literal with XML Schema string datatype
<http://example.org/show/218> <http://www.w3.org/2000/01/rdf-schema#label> "That Seventies Show" . # same as above
<http://example.org/show/218> <http://example.org/show/localName> "That Seventies Show"@en . # literal with a language tag
<http://example.org/show/218> <http://example.org/show/localName> "Cette Série des Années Septante"@fr-be . # literal outside of ASCII range with a region subtag
<http://example.org/#spiderman> <http://example.org/text> "This is a multi-linenliteral with many quotes (""""")nand two apostrophes ('')." .
<http://en.wikipedia.org/wiki/Helium> <http://example.org/elements/atomicNumber> "2"^^<http://www.w3.org/2001/XMLSchema#integer> . # xsd:integer
<http://en.wikipedia.org/wiki/Helium> <http://example.org/elements/specificGravity> "1.663E-4"^^<http://www.w3.org/2001/XMLSchema#double> . # xsd:double
RDF blank nodes in N-Triples are expressed as _:
followed by a blank node label which is a series of name characters.
The characters in the label are built upon PN_CHARS_BASE, liberalized as follows:
_
and [0-9]
may appear anywhere in a blank node label..
may appear anywhere except the first or last character.-
, U+00B7
, U+0300
to U+036F
and U+203F
to U+2040
are permitted anywhere except the first character.A fresh RDF blank node is allocated for each unique blank node label in a document. Repeated use of the same blank node label identifies the same RDF blank node.
_:alice <http://xmlns.com/foaf/0.1/knows> _:bob .
_:bob <http://xmlns.com/foaf/0.1/knows> _:alice .
This section defines a canonical form of N-Triples which has a completely specified layout. The grammar for the language is unchanged.
While the N-Triples syntax allows choices for the representation and layout of RDF data,
the canonical form of N-Triples provides a unique syntactic representation of any triple.
Each code point
can be represented by only one of
UCHAR
,
ECHAR
,
or unencoded character,
where the relevant production allows for a choice in representation.
Each triple is represented entirely on a single line with specified white space.
Canonical N-Triples has the following additional constraints on layout:
subject
,
predicate
, and
object
,
any of which MUST be a single space (U+0020
).http://www.w3.org/2001/XMLSchema#string
MUST NOT use the datatype IRI part of the literal,
and are represented using only STRING_LITERAL_QUOTE.
HEX
MUST use only uppercase letters ([A-F]
).U+0008
(BS
),
U+0009
(HT
),
U+000A
(LF
),
U+000C
(FF
),
U+000D
(CR
),
U+0022
("
), and
U+005C
(\
)
MUST be encoded using ECHAR
.
Characters in the range from U+0000
to U+001F
and U+007F
(DEL
)
that are not represented using ECHAR
MUST be represented by UCHAR
.
All other characters MUST be represented by their native [UNICODE] representation.EOL
MUST be a single U+000A
.EOL
MUST be provided.As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key words MAY, MUST, MUST NOT, and SHOULD in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.
This specification defines conformance criteria for:
A conforming N-Triples document is a Unicode string that conforms to the grammar and additional constraints defined in 5. N-Triples Grammar, starting with the ntriplesDoc
production.
An N-Triples document serializes an RDF graph.
A conforming Canonical N-Triples document is an N-Triples document that follows the additional constraints of Canonical N-Triples.
A conforming N-Triples parser is a system capable of reading N-Triples documents on behalf of an application. It makes the serialized RDF graph, as defined in 6. Parsing, available to the application, usually through some form of API.
The IRI that identifies the N-Triples language is: http://www.w3.org/ns/formats/N-Triples
The media type of N-Triples is application/n-triples
.
The content encoding of N-Triples is always UTF-8.
See N-Triples Media Type for the media type
registration form.
N-Triples has been historically provided with other media types.
N-Triples may also be provided as text/plain
.
When used in this way N-Triples MUST> use the escaped form of any character outside US-ASCII.
As N-Triples is a subset of Turtle an N-Triples document MAY also be provided as text/turtle
.
In both of these cases the document is not an N-Triples document as an N-Triples document is only provided as application/n-triples
.
An N-Triples document is a Unicode [UNICODE] character string encoded in UTF-8.
White space (tab U+0009 or space U+0020) is allowed outside of terminals. Rule names below in capitals indicate where white space is significant.
White space is significant in the production STRING_LITERAL_QUOTE.
A blank line, consisting of only white space and/or a comment,
may appear wherever a triple
production is allowed,
and are treated as white space.
N-Triples allows only horizontal white space (tab U+0009 or space U+0020)
as compared to Turtle [RDF12-TURTLE] which also treats
LF
(U+000A)
and CR
(U+000D)
as white space.
Comments in N-Triples start at '#
'
outside an IRIREF or STRING_LITERAL_QUOTE,
and continue to the end of line
(marked by characters
CR
(U+000D or
LF
(U+000A))
or end of file if there is no end of line after the comment
marker.
Comments are treated as white space.
The EBNF used here is defined in XML 1.0 [EBNF-NOTATION].
Escape sequence rules are the same as Turtle [RDF12-TURTLE].
However, as only the STRING_LITERAL_QUOTE
production is allowed new lines in literals MUST be escaped.
[1] | ntriplesDoc |
::= | triple? ( EOL triple) * EOL? |
[2] | triple |
::= | subject predicate object ". " |
[3] | subject |
::= | IRIREF | BLANK_NODE_LABEL | quotedTriple |
[4] | predicate |
::= | IRIREF |
[5] | object |
::= | IRIREF | BLANK_NODE_LABEL | literal | quotedTriple |
[6] | literal |
::= | STRING_LITERAL_QUOTE ( ( "^^ " IRIREF) | LANGTAG) ? |
[7] | quotedTriple |
::= | "<< " subject predicate object ">> " |
[9] | IRIREF |
::= | "< " ( [ ^ #x00 - #x20 <>"{}|^`\ ] | UCHAR) * "> " |
[10] | BLANK_NODE_LABEL |
::= | "_: " ( PN_CHARS_U | [ 0-9 ] ) ( ( PN_CHARS | ". ") * PN_CHARS) ? |
[11] | LANGTAG |
::= | "@ " [ a-zA-Z ] + ( "- " [ a-zA-Z0-9 ] + ) * |
[12] | STRING_LITERAL_QUOTE |
::= | '" ' ( [ ^ #x22 #x5C #x0A #x0D ] | ECHAR | UCHAR) * '" ' |
[13] | UCHAR |
::= | ( "\u " HEX HEX HEX HEX) | ( "\U " HEX HEX HEX HEX HEX HEX HEX HEX) |
[14] | ECHAR |
::= | "\ " [ tbnrf"' ] |
[15] | PN_CHARS_BASE |
::= | [ A-Z ] |
| |
[ a-z ] |
||
| |
[ #xC0 - #xD6 ] |
||
| |
[ #xD8 - #xF6 ] |
||
| |
[ #xF8 - #x02FF ] |
||
| |
[ #x0370 - #x037D ] |
||
| |
[ #x037F - #x1FFF ] |
||
| |
[ #x200C - #x200D ] |
||
| |
[ #x2070 - #x218F ] |
||
| |
[ #x2C00 - #x2FEF ] |
||
| |
[ #x3001 - #xD7FF ] |
||
| |
[ #xF900 - #xFDCF ] |
||
| |
[ #xFDF0 - #xFFFD ] |
||
| |
[ #x00010000 - #x000EFFFF ] |
||
[16] | PN_CHARS_U |
::= | PN_CHARS_BASE | "_ " |
[17] | PN_CHARS |
::= | PN_CHARS_U | "- " | [ 0-9 ] | #xB7 | [ #x0300 - #x036F ] | [ #x203F - #x2040 ] |
[18] | HEX |
::= | [ 0-9 ] | [ A-F ] | [ a-f ] |
[19] | EOL |
::= | [ #x0D #x0A ] + |
Parsing N-Triples requires a state of one item:
bnodeLabels
— A mapping from string to blank node.This table maps productions and lexical tokens to RDF terms
or components of RDF terms
listed in 6. Parsing:
production | type | procedure |
---|---|---|
BLANK_NODE_LABEL | blank node |
The string after '_: ',
is a key in bnodeLabels.
If there is no corresponding blank node in the map,
one is allocated.
|
IRIREF | IRI | The characters between "<" and ">" are taken, with escape sequences unescaped, to form the unicode string of the IRI. |
LANGTAG | language tag |
The characters following the @ form the unicode string of the language tag.
|
STRING_LITERAL_QUOTE | lexical form |
The characters between the outermost quotation marks (" ) are taken,
with escape sequences unescaped,
to form the unicode string of a lexical form.
|
literal | literal |
The literal has a lexical form of the first rule argument,
STRING_LITERAL_QUOTE ,
and either a language tag of LANGTAG
or a datatype IRI of iri ,
depending on which rule matched the input.
If the LANGTAG rule matched,
the datatype is rdf:langString
and the language tag is LANGTAG .
If neither a language tag nor a datatype IRI is provided,
the literal has a datatype of xsd:string .
|
quotedTriple | quoted triple |
The quoted triple
is composed of the terms constructed from
the subject ,
predicate , and
object productions.
|
An N-Triples document defines an RDF graphs
composed of a set of RDF Triples.
The triple
production
produces a triple defined by the terms constructed for
subject
,
predicate
, and
object
.
This section is non-normative.
The N-Triples format is used to express arbitrary application data, which may include the expression of personally identifiable information (PII) or other information which could be considered sensitive. Authors publishing such information are advised to carefully consider the needs and use of publishing such information, as well as the applicable regulations for the regions where the data is expected to be consumed and potentially revealed (e.g., GDPR, CCPA, others), particularly whether authorization measures are needed for access to the data.
This section is non-normative.
The STRING_LITERAL_QUOTE production allows the use of unescaped control characters. Although this specification does not directly expose this content to an end user, it might be presented through a user agent, which may cause the presented text to be obfuscated due to presentation of such characters.
N-Triples is a general-purpose assertion language; applications may evaluate given data to infer more assertions or to dereference IRIs, invoking the security considerations of the scheme for that IRI. Note in particular, the privacy issues in [RFC3023] section 10 for HTTP IRIs. Data obtained from an inaccurate or malicious data source may lead to inaccurate or misleading conclusions, as well as the dereferencing of unintended IRIs. Care must be taken to align the trust in consulted resources with the sensitivity of the intended use of the data; inferences of potential medical treatments would likely require different trust than inferences for trip planning.
The N-Triples language is used to express arbitrary application data; security considerations will vary by domain of use. Security tools and protocols applicable to text (for example, PGP encryption, checksum validation, password-protected compression) may also be used on N-Triples documents. Security/privacy protocols must be imposed which reflect the sensitivity of the embedded information.
N-Triples can express data which is presented to the user, such as RDF Schema labels. Applications rendering strings retrieved from untrusted N-Triples documents, or using unescaped characters, SHOULD use warnings and other appropriate means to limit the possibility that malignant strings might be used to mislead the reader. The security considerations in the media type registration for XML ([RFC3023] section 10) provide additional guidance around the expression of arbitrary data and markup.
N-Triples uses IRIs as term identifiers. Applications interpreting data expressed in N-Triples SHOULD address the security issues of Internationalized Resource Identifiers (IRIs) [RFC3987] Section 8, as well as Uniform Resource Identifier (URI): Generic Syntax [RFC3986] Section 7.
Multiple IRIs may have the same appearance. Characters in different scripts may look similar (for instance, a Cyrillic "о" may appear similar to a Latin "o"). A character followed by combining characters may have the same visual representation as another character (for example, LATIN SMALL LETTER "E" followed by COMBINING ACUTE ACCENT has the same visual representation as LATIN SMALL LETTER "E" WITH ACUTE). Any person or application that is writing or interpreting data in N-Triples must take care to use the IRI that matches the intended semantics, and avoid IRIs that may look similar. Further information about matching visually similar characters can be found in Unicode Security Considerations [UNICODE-SECURITY] and Internationalized Resource Identifiers (IRIs) [RFC3987] Section 8.
The Internet Media Type (formerly known as MIME Type) for N-Triples is "application/n-triples".
It is recommended that N-Triples files have the extension ".nt" (all lowercase) on all platforms.
It is recommended that N-Triples files stored on Macintosh HFS file systems be given a file type of "TEXT".
This information that follows will be submitted to the IESG for review, approval, and registration with IANA.
This section is non-normative.
This section is non-normative.
The editor of the RDF 1.1 edition acknowledges valuable contributions from Gregg Kellogg, Eric Prud'hommeaux, Dave Beckett, David Robillard, Gregory Williams, Pat Hayes, Richard Cyganiak, Henry S. Thompson, Peter Ansell, Evan Patton and David Booth.
This specification is a product of extended deliberations by the members of the RDF Working Group. It draws upon the earlier specification in [RDF-TESTCASES], edited by Dave Beckett.
This section is non-normative.
The editors of the RDF 1.2 edition acknowledge valuable contributions from Andy Seaborne.
In addition to the editors, the following people have contributed to this specification: Peter F. Patel-Schneider, Pierre-Antoine Champin, and Ted Thibodeau Jr
Members of the RDF-star Working Group Group included Achille Zappa, Adrian Gschwend, Andy Seaborne, Antoine Zimmermann, Dan Brickley, David Chaves-Fraga, Dominik Tomaszuk, Dörthe Arndt, Enrico Franconi, Fabien Gandon, Gregg Kellogg, Gregory Williams, Jesse Wright, Jose Emilio Labra Gayo, Julián Arenas-Guerrero, Olaf Hartig, Ora Lassila, Pasquale Lisena, Peter Patel-Schneider, Pierre-Antoine Champin, Raphaël Troncy, Ruben Taelman, Rémi Ceres, Souripriya Das, Stuart Sutton, Ted Thibodeau, Thomas Pellissier Tanon, Timothée Haudebourg, and Vladimir Alexiev.
Recognize members of the Task Force? Not an easy to find list of contributors.
This section is non-normative.
This section is non-normative.
This section is non-normative.
There are no issues listed in this specification.
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in: