2.5 RDF Serialization: N3 and N-Triples

Though RDF/XML is the serialization technique used in the rest of this book, another serialization technique supported by many RDF applications and tools is N-Triples. This format breaks an RDF graph into its separate triples, one on each line. Regardless of the shorthand technique used within RDF/XML, N-Triples generated from the same RDF graph always come out the same, making it an effective way of validating the processing of an RDF/XML document. For instance, the test cases in the RDF Test Cases document, part of the RDF specification, are given in both the RDF/XML format and the N-Triples format to ensure that the RDF/XML (and the underlying RDF concepts) are consistently interpreted.

Though other techniques for serialization exist, as has been previously discussed, the only serialization technique officially adopted by the RDF specifications is RDF/XML.

N-Triples itself is based on another notation, called N3.

2.5.1 A Brief Look at N3

RDF/XML is the official serialization technique for RDF data, but another notation is also used frequently, which is known as N3 or Notation3. It's important you know how to read it; however, since this book is focusing on RDF/XML, we'll look only briefly at N3 notation.

N3 exists independent of RDF and can extend RDF in such a way as to violate the semantics of the underlying RDF graph. Some prefer N3 to RDF/XML; I am not one of them, primarily because I believe RDF/XML is a more comfortable format for people more used to markup (such as XML or HTML).

The basic structure of an N3 triple is:

subject predicate object .

In this syntax, the subject, predicate, and object are separated by spaces, and the triple is terminated with a period (.). An actual example of N3 would be:

                               <http://purl.org/dc/elements/1.1/creator> Shelley .

In this example, the absolute URIs are surrounded by angle brackets. To simplify this even further, namespace-qualified XML names (QNames) can be used instead of the full namespace, as long as the namespaces are declared somewhere within the document. If QNames are used, the angle brackets are omitted for the predicates:

<bbd:000805.htm> dc:creator Shelley.

To represent multiple triples, with related resources, just list out the triples. Converting the RDF/XML in Example 3-9 into N3 we have:

<bbd:monsters1.htm> pstcn:bio <#monster1> .
<#monster1> pstcn.title "Tale of Two Monsters: Legends .
<#monster1> pstcn.description Part 1 of four-part series on cryptozoology, legends, 
Nessie the Loch Ness Monster and the giant squid" .
<#monster1> pstcn:creator "Shelley" Powers .
<#monster1> pstcn:created "1999-08-01T00:00:00-06:00" .

To represent bnodes or blank nodes, use whatever designation you would prefer to identify the bnode identifier. An example from the RDF Primer is:

exstaff:85740  exterms:address  _:johnaddress .
_:johnaddress  exterms:street   "1501 Grant Avenue" .
_:johnaddress  exterms:city     "Bedford" .
_:johnaddress  exterms:state    "Massachusetts" .
_:johnaddress  exterms:Zip      "01730" .

Though brief, these notes should enable you to read N3 notation all through the RDF specification documents. However, since the focus of this book is RDF/XML, N3 notation won't be used again.

2.5.2 N-Triples

Since N-Triples is a subset of N3, it supports the same format for RDF triples:

subject predicate object .

According to the Extended Backus-Naur Form (EBNF) for N-Triples, a space or a tab separates the three elements from each other, and a space or a tab can precede the elements. In addition, each triple is ended with a period (.) followed by a line-feed or carriage-return/line-feed. An N-Triples file can also contain comments in the following format:

# comment

Each line in an N-Triples file consists of either a triple or a comment, but not both.

As for the triple elements themselves, the subject can consist of either a uriref or a blank node identifier The latter is a value generated for blank nodes within N-Triples syntax, as there can be no blank subjects within legal N-Triples-formatted output. The blank node identifier format is:


where name is a string. The predicate is always a uriref, and the object can be a uriref, a blank node, or a literal.

Given an RDF graph as shown in Figure 2-3, N-Triples would be returned representing both the title triple and the author triple. Adding in a comment, the output in Example 2-1 is valid N-Triples output for the same RDF graph.

Figure 2-3. RDF graph with two RDF triples and one subject
Example 2-1. N-Triples output
# Chapter 2 Example1
<http://burningbird.net/articles/monsters3.htm> . <http://burningbird.net/postcon/
elements/1.0/author> "Shelley Powers" .
<http://www.burningbird.net/articles/monsters3.htm> <http://burningbird.net/postcon/
elements/1.0/title> "Architeuthis Dux" .

Note that angle brackets are used in N-Triples notation only when the object enclosed is a complete, absolute URI. QNames are not enclosed in angle brackets.

A slightly more complex example of N-Triples can be seen in Example 2-2. In this example, four triples are given for one subject, which in this case happens to be a blank node. Since nodes without labels are not allowed in N-Triples format, the RDF parser (NTriple, included with the ARP parser discussed in Chapter 7) generated an identifier to represent the subject in each triple.

Example 2-2. N-Triples output with generated blank node identifier
_:j0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <http://www.webreference.com/
dhtml/hiermenus> .

_:j0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://burningbird.net/
schema/Contains> .

_:j0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> 
"Tutorials and source code about creating hierarchical menus in DHTML" .

_:j0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> .

_:j0 <http://burningbird.net/schema/recommendedBy> "Shelley Powers" .