N-Quads: Extending N-Triples with Context

Latest Version
http://sw.deri.org/2008/07/n-quads/
Last Modified
$Id: index.html 15695 2008-07-18 15:29:29Z richard $
Status
Public draft
Authors
Richard Cyganiak (DERI Galway)
Andreas Harth (DERI Galway)
Aidan Hogan (DERI Galway)

Introduction

A foundational specification of the Semantic Web technology stack is the Resource Description Framework (RDF), a graph-structured data model which uses URIs to name nodes and arc types, arriving at subject-predicate-object triples. N-Triples is a simple line-delimited syntax for RDF graphs.

This document describes N-Quads, a format that extends N-Triples with context. Each triple in an N-Quads document can have an optional context value.

The notion of provenance is essential when integrating data from different sources or on the Web. Therefore, state-of-the-art RDF repositories store subject-predicate-object-context quadruples, where the context typically denotes the provenance of a given statement. The SPARQL query language can query RDF datasets, entire collections of RDF graphs. The context element is also sometimes used to track a dimension such as time or geographic location.

Applications of N-Quads include: Exchange of RDF datasets between RDF repositories, where the fourth element is the URI of the graph that contains each statement. Exchange of collections of RDF documents, where the fourth element is the HTTP URI from which the document was originally retrieved. Publishing of complex RDF knowledge bases, where the original provenance of each statement has to be kept intact.

N-Quads inherit the practical advantages of N-Triples: simple parsing; succinctness compared to alternatives such as reification or multi-document archives; effective streaming and processing with line-based tools.

N-Quads Example

Below is an example N-Quads document. It contains information from two different RDF documents, </alice/foaf.rdf> and </bob/foaf.rdf>.

<http://example.org/alice/foaf.rdf#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <http://example.org/alice/foaf.rdf> .
<http://example.org/alice/foaf.rdf#me> <http://xmlns.com/foaf/0.1/name> "Alice" <http://example.org/alice/foaf.rdf> .
<http://example.org/alice/foaf.rdf#me> <http://xmlns.com/foaf/0.1/knows> _:bnode1 <http://example.org/alice/foaf.rdf> .
_:bnode1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <http://example.org/alice/foaf.rdf> .
_:bnode1 <http://xmlns.com/foaf/0.1/name> "Bob" <http://example.org/alice/foaf.rdf> .
_:bnode1 <http://xmlns.com/foaf/0.1/homepage> <http://example.org/bob/> <http://example.org/alice/foaf.rdf> .
_:bnode1 <http://www.w3. org/2000/01/rdf-schema#seeAlso> <http://example.org/bob/foaf.rdf> <http://example.org/alice/foaf.rdf> .
<http://example.org/bob/foaf.rdf#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <http://example.org/bob/foaf.rdf> .
<http://example.org/bob/foaf.rdf#me> <http://xmlns.com/foaf/0.1/name> "Bob" <http://example.org/bob/foaf.rdf> .
<http://example.org/bob/foaf.rdf#me> <http://xmlns.com/foaf/0.1/homepage> <http://example.org/bob/> <http://example.org/bob/foaf.rdf> .

Grammar, Unicode String Encoding, URI References

An EBNF grammar for N-Quads documents can be derived from the N-Triples grammar by replacing the triple production with a new contextTriple production:

contextTriple ::= subject ws+ predicate ws+ object ( ws+ context )? ws* '.' ws*
context ::= uriref | nodeID | literal

The Unicode character string encoding rules and notes on encoding of URI References from the N-Triples specification apply to N-Quads unchanged.

MIME Type and File Extension

The file extension .nq is recommended for N-Quad documents. The MIME type is text/x-nquads and the encoding is 7-bit US-ASCII.

Serialising SPARQL Datasets as N-Quads

A frequent application of N-Quads is the serialisation of a SPARQL dataset. A SPARQL dataset consists of (i) an RDF graph called the default graph, (ii) zero or more <URI, RDF graph> pairs called named graphs.

A SPARQL dataset is converted to a set of quads by (i) serializing all triples of the default graph as in N-Triples (without a context value), and (ii) serializing all triples in named graphs by using the named graphs's URI as the context for all triples in that graph.

References