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:
<subject> <predicate> <object> <context> .
As opposed to N-Triples, where each triple has the form:
<subject> <predicate> <object> .
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; and 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.
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> .
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 | ::= subjectws+ predicatews+ 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.
The file extension .nq is recommended for N-Quads documents. The media type is text/x-nquads and the encoding is 7-bit US-ASCII. The URI that identifies the N-Quads syntax is http://sw.deri.org/2008/07/n-quads/#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) serialising all triples of the default graph as in N-Triples (without a context value), and (ii) serialising all triples in named graphs by using the named graphs's URI as the context for all triples in that graph.