CONSTRUCT
The CONSTRUCT query form returns a single RDF graph specified by a graph template. The result is
an RDF graph formed by taking each query solution in the solution sequence, substituting
for the variables in the graph template, and combining the triples into a single RDF
graph by set union.
If any such instantiation produces a triple containing an unbound variable or an illegal RDF construct, such as a literal in subject or predicate position, then that triple is not included in the output RDF graph. The graph template can contain triples with no variables (known as ground or explicit triples), and these also appear in the output RDF graph returned by the CONSTRUCT query form.
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@example.org> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name }
WHERE { ?x foaf:name ?name }
creates vcard properties from the FOAF information:
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
<http://example.org/person#Alice> vcard:FN "Alice" .
Templates with Blank Nodes
A template can create an RDF graph containing blank nodes. The blank node labels are scoped to the template for each solution. If the same label occurs twice in a template, then there will be one blank node created for each query solution, but there will be different blank nodes for triples generated by different query solutions.
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:givenname "Alice" .
_:a foaf:family_name "Hacker" .
_:b foaf:firstname "Bob" .
_:b foaf:surname "Hacker" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT { ?x vcard:N _:v .
_:v vcard:givenName ?gname .
_:v vcard:familyName ?fname }
WHERE
{
{ ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } .
{ ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } .
}
creates vcard properties corresponding to the FOAF information:
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
_:v1 vcard:N _:x .
_:x vcard:givenName "Alice" .
_:x vcard:familyName "Hacker" .
_:v2 vcard:N _:z .
_:z vcard:givenName "Bob" .
_:z vcard:familyName "Hacker" .
The use of variable x in the template, which in this example will be bound to blank nodes with labels _:a and _:b in the data, causes different blank node labels (_:v1 and _:v2) in the resulting RDF graph.
Accessing Graphs in the RDF Dataset
Using CONSTRUCT, it is possible to extract parts or the whole of graphs from the target RDF dataset.
This first example returns the graph (if it is in the dataset) with IRI label http://example.org/aGraph; otherwise, it returns an empty graph.
CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/aGraph> { ?s ?p ?o } . }
The access to the graph can be conditional on other information. For example, if the default graph contains metadata about the named graphs in the dataset, then a query like the following one can extract one graph based on information about the named graph:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX app: <http://example.org/ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
CONSTRUCT { ?s ?p ?o } WHERE
{
GRAPH ?g { ?s ?p ?o } .
?g dc:publisher <http://www.w3.org/> .
?g dc:date ?date .
FILTER ( app:customDate(?date) > "2005-02-28T00:00:00Z"^^xsd:dateTime ) .
}
where app:customDate identifies an extension function to turn the date format into an xsd:dateTime RDF term.
Solution Modifiers and CONSTRUCT
The solution modifiers of a query affect the results of a CONSTRUCT query. In this example, the output graph from the CONSTRUCT template is formed from just two of the solutions from graph pattern matching. The
query outputs a graph with the names of the people with the top two sites, rated by
hits. The triples in the RDF graph are not ordered.
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix site: <http://example.org/stats#> .
_:a foaf:name "Alice" .
_:a site:hits 2349 .
_:b foaf:name "Bob" .
_:b site:hits 105 .
_:c foaf:name "Eve" .
_:c site:hits 181 .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX site: <http://example.org/stats#>
CONSTRUCT { [] foaf:name ?name }
WHERE
{ [] foaf:name ?name ;
site:hits ?hits .
}
ORDER BY desc(?hits)
LIMIT 2
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:x foaf:name "Alice" .
_:y foaf:name "Eve" .
CONSTRUCT WHERE
A short form for the CONSTRUCT query form is provided for the case where the template
and the pattern are the same and the pattern is just a basic graph pattern (no FILTERs and no complex graph patterns are allowed in the short form). The keyword WHERE is required in the short form.
The following two queries are the same; the first is a short form of the second.
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT WHERE { ?x foaf:name ?name }
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT { ?x foaf:name ?name }
WHERE
{ ?x foaf:name ?name }
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes material copied from or derived from SPARQL 1.1 Query.