Everything You Need to Know About RDF
A comprehensive guide to the Resource Description Framework
The Resource Description Framework (RDF) lets us describe people, documents, and other resources in a form different applications can exchange. Its data model describes which facts the graph contains. Several text formats can encode those same facts.
Natural Language vs. Machine-Readable Data
Consider a fact I can state in a sentence:
This statement is straightforward for humans to understand and express. Representing it in a machine-readable format is another matter: the same data can be written in several syntaxes, and even within a single one, such as XML, one of the most prevalent data representation languages, it can take various forms. The tabs below show three XML variants alongside the same fact in Turtle and JSON-LD, two RDF syntaxes we will meet later in this post:
The XML variants put the owner's name in different places. A reader needs the producer's conventions to interpret those fields. The Turtle and JSON-LD snippets both identify the blog, Ahmad, and the ownership relationship by their full IRIs (Internationalized Resource Identifiers). In Turtle, the prefix declarations let us abbreviate those identifiers.
Constituents of an RDF Triple
A triple describes one fact. Its subject is the resource we're describing. Its predicate names the relationship or attribute. Its object gives the related resource or value.
| Subject | Predicate | Object |
|---|---|---|
http://assaf.website/blog | aa:owner | :ahmadassaf |
In the graph, the arrow starts at the blog and points to Ahmad. Subjects can be IRIs or blank nodes, predicates must be IRIs, and objects can be IRIs, literals, or blank nodes. A literal holds a value directly, such as the title of the blog. A blank node lets us describe a resource without giving it a global name.
Ahmad also administers his blog. That gives us a second relationship to record.
An RDF graph is a set of triples. Here are two copies of the administrator statement:
The two lines produce one triple because their subjects, predicates, and objects are identical. Recording two reports or two appointments requires representing each one separately, as we'll see in the reification and appointment examples.
RDF uses three kinds of terms for nodes.
URIs
A Uniform Resource Identifier (URI) can name a resource so that other statements can refer to it. A URI can identify a person, a web page, a book, or a place. Using an HTTP identifier does not guarantee that requesting it will return a representation of the resource.
Two people can share a display name. Separate identifiers let us say which person a statement concerns.
Strictly speaking, RDF 1.1 calls these identifiers IRIs (Internationalized Resource Identifiers, RFC 3987), the generalization of URIs to the full Unicode repertoire. An IRI is a Unicode string that does not contain any control characters and that produces a valid absolute URI, with an optional fragment identifier, when subjected to the encoding that consists of:
- encoding the Unicode string as UTF-8 giving a sequence of octet values.
- %-escaping octets that do not correspond to permitted US-ASCII characters.
Throughout this post I keep the informal, familiar word "URI" for these identifiers, as most tooling and writing on RDF still does.
Literals
A literal gives us a value directly. In RDF 1.1 every literal has a datatype. In Turtle, "Semantics" is a string with the datatype xsd:string. Adding a language tag, as in "Semantics"@en, gives it the datatype rdf:langString. We can also write a datatype explicitly. For a string, I use the full IRI from the XML Schema datatype namespace like this:
Here the fragment #string identifies the datatype. The quotation marks matter. In Turtle, "30" is a string of two characters. Written without quotes, 30 is an xsd:integer. Turtle also has shorthand for decimals, doubles, and the Boolean values true and false. Turtle literals
For text, Turtle accepts single or double quotes, as in "simple literal". Triple quotes, as in """long literal""", let a string span several lines. Numbers have a different wrinkle. Let's think of the datatype for floating-point numbers. The text "5.1" represents the number 5.1, but so do "5.1000" and "05.10". These different spellings of the same value are called lexical representations.
A datatype tells us how a lexical representation maps to a value. RDF reuses the XML Schema (W3C) datatypes, including xsd:string, xsd:float, xsd:double, xsd:integer, and xsd:date. RDF can also contain custom datatypes that (you guessed it!) are simply named with a URI. A quoted string without a language tag or explicit datatype is an xsd:string: "Ahmad Assaf" and "Ahmad Assaf"^^xsd:string are the same literal. (Under the original RDF specification an untyped "plain literal" was a distinct thing from a typed string; RDF 1.1, published in 2014, removed that distinction, and tools have supported this for years.)
The semantics of RDF takes language tags and datatypes into account. This means two things. First, a literal with a language tag (an rdf:langString) is different from the same text without one (an xsd:string), and literals with different language tags are different from each other. These three statements say three different things and none can be inferred from the others:
So, a literal with a language tag is never the same as a literal without one. The second part of the semantics of literals is that two typed literals with different lexical representations can denote the same value. For the two literals below, xsd:float maps both lexical forms to the same number.
In this hypothetical example, both literals give Ahmad an age of 20. Their lexical forms differ, so "20"^^xsd:float and "20.000"^^xsd:float are distinct RDF terms, and the graph contains two distinct triples expressing that age. Note that if the float datatype were not specified, both literals would be xsd:strings, the triples would not be equivalent, and the textual representation of the 20 would be maintained as part of the information content. RDF literal equality
Sometimes the value of a property needs to be a fragment of XML, or text that might contain XML markup. RDF/XML provides a special notation to make it easy to write literals of this kind. This is done using the rdf:parseType attribute (we will meet its other values, Resource and Collection, further on). Giving an element the attribute rdf:parseType="Literal" indicates that the contents of the element are to be interpreted as an XML fragment:
In Turtle notation the datatype used for XML literals is rdf:XMLLiteral.
Blank Nodes
Subjects or objects can be modeled as blank nodes: anonymous resources that denote the existence of an individual without naming it. I cover them in detail in the Blank Nodes (B Nodes) section below.
RDF Representation
We can draw an RDF fact or serialize it as text that a parser can read. The ownership triple gives us a way to check that we're describing the same graph in each notation.
Labeled Directed Graph
This is a visual way of modeling RDF as a Node-Edge-Node Triple. It's directed, as the direction of the edge is significant and always points towards the object.
Directionality matters in RDF triples. Defining reverse relationships is possible, but not always necessary. This is the territory of the Web Ontology Language (OWL), which offers a simple way to define the "opposite" property using the inverseOf relationship.
RDF/XML Notation
RDF/XML uses XML elements and attributes to encode triples. A namespace declaration lets us write a short name for a predicate whose full IRI occurs throughout the document.
The namespace declaration for aa: supplies the beginning of each IRI in our example vocabulary. When a parser sees aa:owner it can recover the full predicate IRI without fetching a vocabulary document.
This encodes the same ownership triple as the opening Turtle and JSON-LD. The subject is the resource named in rdf:about. The property element names the predicate, and rdf:resource supplies the object IRI. A literal value goes inside the property element instead. We can describe both the blog and Ahmad in the same document:
In an RDF/XML document there are two types of XML nodes:
- resource XML nodes: the subjects and objects of statements. They usually are
rdf:Descriptiontags that have anrdf:aboutattribute on them giving the URI of the resource they represent, and they contain within them property XML nodes (and nothing else). In this example, therdf:Descriptionnodes are the resource nodes. - property XML nodes: the nodes nested inside resource XML nodes, each describing a property of the enclosing resource.
In the example above, the foaf:name element gives Ahmad's name. The foaf: prefix expands to the namespace declared at the top of the document. The value of the rdf:datatype attribute follows a different rule. It takes an IRI, so I write the full XML Schema IRI for string there. Writing rdf:datatype="xsd:string" would name a different datatype whose IRI starts with xsd:, even if I declared that prefix. For relative references, I can set a base IRI with xml:base:
the relative reference #ahmadassaf resolves to http://assaf.website/triples#ahmadassaf:
Instead of using rdf:about and then putting the hash # in front of the fragment URI, I can use rdf:ID, which has the hash built in, so that I can write:
RDF/XML is difficult to read, verbose, and not particularly flexible. It was the original standard serialization for RDF, and it benefited from the ubiquity of XML tooling in browsers and parsers; today, however, Turtle and JSON-LD dominate in practice.
N-Triples and N3 Notation
N-Triples is the simplest listing of triples: one complete triple per line, full URIs and no abbreviations. It is a non-XML serialization that is more compact than RDF/XML but for complex and large models it can become very expensive.
N3 (Notation3) is a superset of this syntax designed with human readability in mind. It has some syntactic sugar that allows further abbreviations. If two statements repeat the same subject and predicate, we can separate their objects with a comma. Suppose a blog had a second owner:
And if the same subject is repeated, but with different predicates, one may use semicolons as in the example:
Turtle (Terse RDF Triple Language)
A simplified form of the N3 notation. URIs are wrapped in angle brackets and literals in quotation marks. Every triple ends with a period, and whitespace or indentation will be ignored.
We can also write the ownership fact and blog title with Turtle prefixes. aa: supplies the vocabulary namespace, while the empty prefix : abbreviates our directory identifiers.
The semicolon carries the subject forward to the title triple. The declaration for : expands :ahmadassaf to http://assaf.website/triples#ahmadassaf. A base IRI serves a different purpose. With this @base, the relative IRI <#ahmadassaf> names the same person. The current base can change within a document:
Notes
- Turtle strings can use the usual backslash escapes (
\t,\n,\", etc.), and both strings and URIs can write Unicode code points as\uXXXXor\UXXXXXXXX; the string escapes are not allowed inside URIs. - Comments in Turtle take the form of
#, outside a relative URI or strings, and continue to the end of the line. - The
,symbol may be used to repeat the subject and predicate of triples that only differ in the object RDF term.
- The
;symbol may be used to repeat the subject of triples that vary only in predicate and object RDF terms.
Blank Nodes (B Nodes)
A blank node lets us describe something without giving it an IRI. A ground RDF graph contains no blank nodes. Suppose I want to record two influences on my blog at http://assaf.website/blog. I have a name and a URL for each one. Blog A is at http://www.blogA.com and Blog B is at http://www.blogB.com. If I attach both names and both links directly to my blog, which link goes with “Blog A”? Here I give each influence its own blank node so the name and link stay together:
The dashed circular markers are blank nodes: they do not have a public name and act as connection points for the related facts.
Here is the Blog A branch of that diagram, first in RDF/XML and then in Turtle. Blog B follows the same pattern.
In RDF/XML the blank nodes are denoted by the rdf:parseType="Resource" attribute; in Turtle it's much easier:
We simply introduce blank nodes in Turtle by using the brackets [ ]. So far we have assumed that Blank Nodes do not have names. This is not always the case, as sometimes I need to give a label or a name to a blank node to be able to reference it locally. The value of rdf:nodeID is a label that is local to the document: it is never resolved against the base URI and cannot be referenced from outside it.
In Turtle we use _:, the reserved blank-node prefix, followed by the Blank Node name:
So Blank Nodes do NOT need to be dereferenceable and accessed from the outside world; however, they can have IDs or names that allow them to be referenced in an RDF document or model.
Data Structures in RDF
Suppose we want to group the members of a blog administrator roster. We use aa:adminRoster to connect the blog to the group. Earlier, aa:hasAdmins connected the blog directly to an individual. To find the people in a roster, we follow the group's membership links. RDF offers two ways to represent such a group.
Containers
An open list of elements possibly including duplicate members where new entries (additions) are possible. Note that the container resource (which may either be a blank node or a resource with a URIref) denotes the group as a whole. The members of the container can be described by defining a container membership property for each member with the container resource as its subject and the member as its object. These container membership properties have names of the form rdf:_n where n is a decimal integer greater than zero, with no leading zeros, e.g., rdf:_1, rdf:_2, rdf:_3 and so on, and are used specifically for describing the members of containers. Container resources may also have other properties that describe the container, in addition to the container membership properties and the rdf:type property.
We notice that the container node has an rdf:type of rdf:Seq, which means that this is a sequence: a container where the order of the items is important. Each item in the sequence has a unique property rdf:_1, rdf:_2 ... etc. In Turtle, sequences are represented as:
The brackets above give the roster a blank node. If I want another document to refer to this roster, I can give it an IRI, :administrators. Its members and their order stay the same:
With that same named roster, we can shorten the Turtle by carrying its subject forward:
The RDF/XML below uses the anonymous roster from the first Turtle example:
RDF/XML provides rdf:li as a convenience element to avoid having to explicitly number each membership property. The numbered properties rdf:_1, rdf:_2 and so on are generated from the rdf:li elements in forming the corresponding graph. The element name rdf:li was chosen to be mnemonic with the term "list item" from HTML. We notice that we referenced our blog by using the empty prefix :, and we referred to the container with the brackets [], that is, as a Blank Node, but we added the rdf:type of rdf:Seq to that blank node. In RDF, a shorthand for rdf:type is the letter a (as in natural English: saying "this is a cat" means this is something with a type of cat). We have seen so far the sequential container, which denotes an ordered group of elements, but we also have other types of containers:
rdf:Bag: This is an unordered container of elements, possibly including duplicate members.rdf:Alt: Defines alternatives of elements where only one element of the given alternatives is relevant to the application (a group of resources or literals that are alternatives, typically for a single value of a property). An Alt container is intended to have at least one member, identified by the propertyrdf:_1. This member is intended to be considered as the default or preferred value. Other than the member identified asrdf:_1the order of the remaining elements is not significant.
Collections
A collection records its members in order. Each cell has a head (rdf:first) and a tail (rdf:rest), and the last tail points to rdf:nil. Following these links gives us the members of this particular list. To append a member we would have to change the tail that currently points to rdf:nil.
Turtle’s list shorthand expands into triples using rdf:first and rdf:rest. It adds no explicit rdf:type rdf:List triple for each cell. An RDFS reasoner can classify the cells as rdf:List using the vocabulary definitions for those predicates. RDF Schema collections
However, in Turtle there are shortcuts to simplify the syntax. We do that by using a new type of brackets, ( ):
RDF/XML provides a special notation to make it easy to describe collections using graphs of this form. In RDF/XML, a collection can be described by a property element that has the attribute rdf:parseType="Collection", and that contains a group of nested elements representing the members of the collection. RDF/XML provides the rdf:parseType attribute to indicate that the contents of an element are to be interpreted in a special way. In this case, the rdf:parseType="Collection" attribute indicates that the enclosed elements are to be used to create the corresponding list structure in the RDF graph:
The collection of RDF statements (RDF Triples) forms an RDF graph. The assertion of an RDF graph (assertion means that a term evaluates to true) amounts to the assertion of all triples in it, this means a conjunction (logical AND) between all the statements corresponding to all the triples in the graph.
The datastore that stores these triples is often called a triple store
Semantic Web practitioners found it very difficult to deal with large amounts of triples for application development. There are lots of reasons that you would want to segment different subsets of triples from each other (simplified access control, simplified updating, trust, etc.), and vanilla RDF made segmentation tedious.
Named Graphs
An RDF dataset contains one default graph, which has no name, and zero or more named graphs. Each named graph pairs a graph name with an RDF graph. When referring to a triple in a named graph we often use a 4-tuple notation (often referred to as quad) instead of the standard 3-tuple one:
When using named graphs, TriG is the standard serialization (a W3C Recommendation since 2014). It's the same as Turtle except that statements in a single graph are grouped with {}:
The blog's title sits outside the braces, in the default graph. Inside :blogGraph are the administrator statement and the triples that describe its list of administrators. Both graphs refer to the same blog, http://assaf.website/blog. If we removed the title triple, this example would have an empty default graph and one named graph.
The same triple can appear in several named graphs, with its membership recorded in each. The name :blogGraph identifies a graph within this dataset. Tracking authorship requires explicit source metadata, and the application decides which sources to trust and who can read or change each graph. RDF datasets
RDF Reification
Suppose a source reports that Ahmad administers his blog. We want to record who made that statement. RDF's reification vocabulary lets us describe a triple with a resource of type rdf:Statement. Three predicates identify the described triple's parts: rdf:subject, rdf:predicate, and rdf:object.
Here is the administrator statement on its own:
Give the statement description its own IRI, :triple12345. The dc:creator triple attributes it to http://assaf.website/85740. We have an identifier for that source, but no name.
Those five Turtle triples describe and attribute the statement. Loading them alone does not assert that Ahmad administers the blog. The RDF/XML below includes those five and the underlying administrator statement, for a total of six triples. Its first rdf:Description makes the assertion. The rdf:Statement element describes it.
The subject of these reification triples is a URI ref formed by resolving the fragment #triple12345 given in rdf:about against the base URI of the document (given in the xml:base declaration), i.e. http://assaf.website/triples#triple12345. However, you can generate the same graph (Reification) more concisely by using the rdf:ID attribute on the property element; rdf:ID has the # built in, so rdf:ID="triple12345" under this base names the same resource:
This shorter XML still produces all six triples. To compare two serializations, count which facts they assert as well as which statements they describe. RDF/XML reification syntax
When did Ahmad start administering the blog?
Who administers the blog? Follow aa:hasAdmins from <http://assaf.website/blog> and we get :ahmadassaf. Who reported that Ahmad is the administrator? For that, we start at :triple12345, the statement we described above. Its dc:creator points to <http://assaf.website/85740>. The example gives us an identifier for the source, but no name.
We still don’t know when Ahmad took on the role. Even if we knew exactly when the source made that statement, Ahmad could have been running the blog for years by then.
Suppose we later find an appointment record that gives 1 September 2020 as his first day. We can give that appointment its own IRI and use a few terms from our aa: vocabulary to record who took on the role, for which blog, and when.
If Ahmad steps down and later returns, where does the second start date go? On a second appointment. Both appointments point to the same Ahmad and the same blog, and we can look up when each term began.
N-ary relations
The appointment is an example of an n-ary relation. A binary relation connects two things, as aa:hasAdmins connects the blog to Ahmad. Here we also need the start date to belong to that particular appointment. We introduce aa:AdminAppointment as a class and create :appointment1 as one instance, with properties linking it to the blog, Ahmad, and the date. Each property still forms an ordinary RDF triple. This follows the first pattern in the W3C Working Group Note on n-ary relations. Defining N-ary Relations on the Semantic Web
:triple12345 describes the statement that Ahmad administers the blog, so its creator tells us who made that statement. :appointment1 represents one term as administrator, so its start date tells us when that term began. Attributing the start date to the same source would require additional source information. N-ary relations and reification in RDF
Wrap Up
Another directory can describe my blog using its own prefixes. To combine its description with mine, I look at the full identifiers. If its owner IRI expands to http://assaf.website/triples#ahmadassaf, the ownership statement and my foaf:name statement meet at the same person. Joshua Tauberer explains this use of shared identifiers in What is RDF and what is it good for?.
How I share those statements depends on what the reader needs. RDFa lets me put the ownership triple in the HTML of my blog. N-Triples puts one triple on each line, which is handy when inspecting a graph from DBpedia with command-line tools. If I send the blog's title in the default graph and its administrator roster in :blogGraph, I can use TriG to keep those graph boundaries in the file.
The opinions and views expressed on this blog are solely my own and do not reflect the opinions, views, or positions of my employer or any affiliated organizations. All content provided on this blog is for informational purposes only.
I also offer consulting and mentoring in AI strategy, engineering leadership, and scaling teams. Get in touch if I can help.