A number of different serialization formats exist for writing down RDF

graphs. However, different ways of writing down the same graph lead
to exactly the same triples, and are thus logically equivalent.
In this section we briefly introduce, through annotated examples, the following formats:
Turtle family of RDF languages
(N-Triples,
Turtle,
TriG and
N-Quads);
JSON-LD (JSON-based RDF syntax);
RDFa (for HTML and XML embedding);
RDF/XML (XML syntax for RDF).
In this subsection we introduce four RDF languages
which are closely related. We start with N-Triples, as it provides
basic syntax for writing down RDF triples. The Turtle syntax
extends this basic syntax with various forms of syntactic sugar to improve
readability. Subsequently we discuss TriG and N-Quads, which are
extensions respectively of Turtle and
N-Triples to encode multiple graphs. Together, these four are
referred to as the "Turtle family of RDF languages".
N-Triples
N-Triples [] provides a simple line-based, plain-text way for serializing RDF
graphs. The informal graph in can be represented in N-Triples in the
following way:
Each line represents a triple. Full IRIs are enclosed in angle brackets
(). The period at the end of the line signals the
end of the triple. In line 3 we see an example of a literal, in this case a date. The
datatype is appended to the literal through a delimiter. The date
representation follows the conventions of the XML Schema datatype
date.
Because string literals are so ubiquitous N-Triples allows the user to
omit the datatype when writing a string literal. Thus, in line 5 is equivalent to
.
In case of language-tagged strings the tag
appears directly after the string, separated by a
symbol, e.g. (the French name of the Mona
Lisa).
The figure below shows the triples resulting from the example:
Fig.
Note that the seven lines in the N-Triples example correspond to the seven
arcs in the diagram above.
N-Triples is often used for exchanging large amounts of RDF and for
processing large RDF graphs with line-oriented text processing
tools.
Turtle
Turtle [] is an extension of N-Triples.
In addition to the basic N-Triples syntax, Turtle
introduces a number of syntactic shortcuts, such as
support for namespace prefixes, lists and shorthands for datatyped
literals. Turtle provides a trade-off between ease of
writing, ease of parsing and readability. The graph shown in
can be
represented in Turtle as follows:
The Turtle example is logically equivalent to the N-Triples
example. Lines 1-6 contain a number of directives which provide shorthands for
writing down IRIs. Relative IRIs (such as on line 8) are
resolved against a base IRI, specified here in line 1.
Lines 2-6 define IRI prefixes (such as ), which can
be used for prefixed names (such as ) instead of full IRIs.
The corresponding IRI is constructed by replacing the prefix with its
corresponding IRI (in this example stands for
).
Lines 8-12 show how Turtle provides a shorthand for a set of
triples with the same subject. Lines 9-12 specify the predicate-object
part of triples that have as
their subject. The semicolons at the end of lines 9-11 indicate that
the predicate-object pair that follows it is part of
a new triple that uses the most recent subject shown in the data — in
this case .
Line 9 shows an example of a special kind of syntactic sugar. The triple
should informally be read as "Bob (is) a Person". The
predicate
is a shorthand for the property which models the
instance relation (see Table 1).
The shorthand is intended to match the human
intuition about .
Representation of blank nodes
Below we see two syntactic variants for writing down blank nodes, using the
earlier cypress tree example.
The term is a blank node. It represents an
unnamed resource depicted in
the Mona Lisa painting; the unnamed resource is an instance of the
class. The example above provides concrete syntax
for the informal graph in .
Turtle also has an alternative notation for blank nodes, which
does not require the use of syntax like :
Square brackets represent here a blank node. Predicate-object pairs within
the square brackets are interpreted as triples with the blank node as
subject. Lines starting with '#' represent
comments.
For more details about the syntax of Turtle please consult the Turtle specification [].
TriG
The syntax of Turtle supports only the specification of single
graphs without a means for "naming" them. TriG [] is an
extension of Turtle enabling the specification of
multiple graphs in the form of an RDF dataset.
The multiple-graphs version of our example
can be specified in TriG as follows:
This RDF dataset contains two named graphs. Lines 8 and 17 list
the names of these two graphs. The triples in the named graph are
placed in between matching curly braces (lines 9 & 15, 18 &
25). Optionally you can precede the graph name with the keyword
. This may improve readability, but it is mainly
introduced for alignment with SPARQL Update [].
The syntax of the triples and of the directives at the top conforms to
the Turtle syntax.
The two triples specified on lines 27-29 are not part of any
named graph. Together they form the unnamed ("default") graph of this RDF
dataset.
The figure below shows the triples resulting from this example.
Fig.
N-Quads
N-Quads [] is a simple extension to N-Triples to enable the exchange of RDF
datasets. N-Quads allows one to add a fourth element to a line, capturing
the graph IRI of the triple described on that line. Here is the
N-Quads version of the TriG example above:
The nine lines in the N-Quads example correspond to the nine
arcs in . Lines 1-7 represent quads, where the first
element constitutes the graph IRI. The part of the quad after the
graph IRI specifies the
subject, predicate and object of the statement, following the syntactic
conventions of N-Triples. Lines 8 and 9 represent the statements in the unnamed (default)
graph, which lack a fourth element and thus constitute regular triples.
Like N-Triples, N-Quads is typically used for exchanging large RDF datasets and for
processing RDF with line-oriented text processing tools.
JSON-LD []
provides a JSON syntax for RDF graphs and datasets.
JSON-LD can be used to transform JSON documents to RDF with
minimal changes. JSON-LD offers universal identifiers for
JSON objects, a mechanism in which a JSON document can refer to
an object described in another JSON document elsewhere on the
Web, as well as datatype and language handling. JSON-LD
also provides a way to serialize RDF datasets
through the use of the
@graph keyword.
The following JSON-LD example encodes the graph of :
The key on line 2
points to a JSON document
describing how the document can be mapped to an RDF graph (see below).
Each JSON object corresponds to an RDF resource. In this example
the main resource being described is
, as
specified on line 3, through the use of the keyword.
The keyword, when used as a key in a JSON-LD document, points
to an IRI identifying the resource corresponding to the current JSON object.
We describe the type of this resource on line 4, its birth date
on line 5 and one of its friends on line 6. From line 7 to 12 we describe
one of its interests, the Mona Lisa painting.
To describe this painting we create a
new JSON object on line 7 and associate it with the Mona Lisa IRI in Wikidata
on line 8. We then describe various properties of that painting
from line 9 to line 11.
The JSON-LD context used in this example is given below.
This context describes how a JSON-LD document can be mapped
to an RDF graph. Lines 4 to 9 specify how to map
, and
to types and properties in the FOAF namespace defined
on line 3. We also specify on line 8 that the
key has a value that will be interpreted as an IRI, through
the use of the and keywords.
From line 10 to line 12 we map to
a schema.org property IRI and specify that its value can
be mapped to an datatype.
From line 14 to line 23 we describe how to map
, and
to Dublin Core property IRIs. The
keyword on line 21 is used to specify that, whenever we
encounter in a JSON-LD document using this
context, we should map it to an RDF triple which subject is the
IRI, which property is and
which object is the resource corresponding to the parent JSON object.
RDFa [] is an RDF syntax that can be used to embed RDF data within
HTML and XML documents. This enables, for example, search engines to aggregate
this data when crawling the Web and use it to enrich search
results (see, e.g., schema.org
and Rich
Snippets).
The HTML example below encodes the
RDF graph depicted in :
The example above contains four special RDFa attributes to enable
specification of RDF triples within HTML: ,
, and .
The attribute in line 1 specifies IRI
shorthands in a similar fashion as the Turtle prefixes. Strictly
speaking, these particular prefixes could have been omitted, as RDFa has a
list of predefined
prefixes which includes the ones used in this example.
The elements in lines 4 and 14 have a
attribute specifying the IRI about which RDF statements can be
made within this HTML element. The meaning of the
attribute in line 4 is similar to the shorthand in
Turtle: the subject is an
instance () of the class .
In line 6 we see a attribute; the value
of this attribute () is interpreted as an RDF
property IRI; the value of the attribute
() is
interpreted here as the object of the triple. Thus, the RDF statement
that results from line 6 is:
In line 7 we see a triple with as object a literal value. The
attribute is specified here on the HTML
element. HTML requires that the content of the time
element should be some valid time
value. By using the built-in HTML semantics of the
element RDFa can interpret
the value as an without an explicit datatype declaration.
In lines 10-11 we see the attribute also being used for
specifying the object of a triple. This approach is used when the object is an
IRI and the IRI itself is not part of the HTML content (such as an
attribute). Line 16 contains a second example of a literal ("Mona
Lisa"), defined here as content of the attribute. If
RDFa cannot infer the datatype of the literal, it will assume the
datatype to be .
It is not always possible to define RDF statements as part of the
HTML content of the document. In that case it is possible to use HTML
constructs that do not render content to specify a triple. An example
can be found on lines 22-23. The HTML element on
line 23 is used here to specify what
the subject of the Europeana video (line 22) is.
The use of RDFa in this example is limited to RDFa Lite
[]. For more information about RDFa please consult the RDFa
Primer [].
RDF/XML [] provides an XML syntax for RDF
graphs. When RDF was originally developed in the late 1990s, this was its
only syntax, and some people still call this syntax "RDF". In 2001, a
precursor to Turtle called "N3" was proposed, and gradually the other
languages listed here have been adopted and standardized.
The RDF/XML example below encodes the
RDF graph depicted in :
In RDF/XML RDF triples are specified within an XML element
(lines 2 and 20). The attributes of the
start tag (lines 3-6) provide a shorthand for writing down
names of XML elements and attributes. The XML element
(short for
)
is used to define sets of triples that have as subject the IRI
specified by the attribute. The first description
block (line 7-12) has four sub-elements. The name of the subelement is
an IRI representing an RDF property, e.g., (line 8). Here, each
subelement represents one triple.
In cases where the object of the triple is also an IRI
the property subelement has no content and the object IRI is specified
using the attribute (lines 8, 10-11, 15 and
18). For example, line 10 corresponds to the triple:
When the object of the
triple is a literal the literal value is entered as content of the
property element (lines 9 and 14). The datatype is specified as
attribute of the property element (line 9). If the datatype is
omitted (line 14) and no language tag is present the literal is
considered to have the datatype .
The example shows the baseline syntax; please consult the RDF/XML
document [] for a more in-depth treatment of the syntax. It
might seem strange that the attribute values contain full IRIs,
despite the fact that for some of these namespace prefixes were
defined. This is because these prefixes can only be used for XML
element and attribute names.
2025年国内旅游收入预计将突破历史新高,以下是综合多方数据的预测与分析:
一、核心数据预测

工作时间:8:00-18:00
电子邮件
扫码二维码
获取最新动态
