Specifying RDF Datasets
A SPARQL query may specify the dataset to be used for matching by using the FROM clause and the FROM NAMED clause to describe the RDF dataset. If a query provides such a dataset description,
then it is used in place of any dataset that the query service would use if no dataset
description is provided in a query. The RDF dataset may also be specified in a SPARQL protocol request, in which case the protocol description overrides any description in the query itself.
A query service may refuse a query request if the dataset description is not acceptable
to the service.
The FROM and FROM NAMED keywords allow a query to specify an RDF dataset by reference; they indicate that
the dataset should include graphs that are obtained from representations of the resources
identified by the given IRIs (i.e. the absolute form of the given IRI references).
The dataset resulting from a number of FROM and FROM NAMED clauses is:
- a default graph consisting of the RDF merge of the graphs referred to in the
FROMclauses, and - a set of (IRI, graph) pairs, one from each
FROM NAMEDclause.
If there is no FROM clause, but there is one or more FROM NAMED clauses, then the dataset includes an empty graph for the default graph.
Specifying the Default Graph
Each FROM clause contains an IRI that indicates a graph to be used to form the default graph.
This does not put the graph in as a named graph.
In this example, the RDF Dataset contains a single default graph and no named graphs:
# Default graph (located at http://example.org/foaf/aliceFoaf)
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
FROM <http://example.org/foaf/aliceFoaf>
WHERE { ?x foaf:name ?name }
| name |
|---|
| "Alice" |
If a query provides more than one FROM clause, providing more than one IRI to indicate the default graph, then the default
graph is the RDF merge of the graphs obtained from representations of the resources identified by the given
IRIs.
Specifying Named Graphs
A query can supply IRIs for the named graphs in the RDF Dataset using the FROM NAMED clause. Each IRI is used to provide one named graph in the RDF Dataset. Using the
same IRI in two or more FROM NAMED clauses results in one named graph with that IRI appearing in the dataset.
# Graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .
...
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
...
The FROM NAMED syntax suggests that the IRI identifies the corresponding graph, but the relationship
between an IRI and a graph in an RDF dataset is indirect. The IRI identifies a resource,
and the resource is represented by a graph (or, more precisely: by a document that
serializes a graph). For further details see [Architecture of the World Wide Web, Volume One].
Combining FROM and FROM NAMED
The FROM clause and FROM NAMED clause can be used in the same query.
# Default graph (located at http://example.org/dft.ttl)
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://example.org/bob> dc:publisher "Bob Hacker" .
<http://example.org/alice> dc:publisher "Alice Hacker" .
# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?who ?g ?mbox
FROM <http://example.org/dft.ttl>
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
WHERE
{
?g dc:publisher ?who .
GRAPH ?g { ?x foaf:mbox ?mbox }
}
The RDF Dataset for this query contains a default graph and two named graphs. The
GRAPH keyword is described below.
The actions required to construct the dataset are not determined by the dataset description
alone. If an IRI is given twice in a dataset description, either by using two FROM clauses, or a FROM clause and a FROM NAMED clause, then it does not assume that exactly one or exactly two attempts are made
to obtain an RDF graph associated with the IRI. Therefore, no assumptions can be made
about blank node identity in triples obtained from the two occurrences in the dataset
description. In general, no assumptions can be made about the equivalence of the graphs.
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes material copied from or derived from SPARQL 1.1 Query.