问题
I'm trying to use SPARQL CONSTRUCT to strip a set of data from dbpedia - I'm only interested in a set of Artists, and I want Sesame as small as possible for speed.
What I thought I could do is use CONSTRUCT to get every predicate for a given artist. I can get the first CONSTRUCT clause working to make sure I get type "Person", but that only gives me triples satisfying that clause - I want their names, labels, birthPlaces etc to. My query below is trying to capture Monet's name in the second CONSTRUCT clause? If I have it right, this would give me a triple of
<http://dbpedia.org/resource/Claude_Monet>
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://xmlns.com/foaf/0.1/Person>
and a triple like this
<http://dbpedia.org/resource/Claude_Monet>
<http://xmlns.com/foaf/0.1/name>
"Claude Monet"@en
How do I get my query to use the object of Monet's name as a variable to use where I am inserting empty quotes please? Here's the query
PREFIX purl: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT {
?s a foaf:Person .
?s foaf:name ""
} WHERE {
?s foaf:surname "Monet"@en .
?s purl:description "Painter"@en
} LIMIT 100
Any help really appreciated
Mike
回答1:
Here we are, this is it. Once I figured the variable bindings between the CONSTRUCT and WHERE it was actually straightforward.
Each WHERE statement selects those values from the repo, and requires a matching template statement in the CONSTRUCT referencing the same variable. The value is just replaced. Obvious I suppose.
Note to self - MUST stop thinking like RDBMS.
PREFIX purl: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbont: <http://dbpedia.org/ontology/>
PREFIX w3: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT
{
?s a foaf:Person .
?s foaf:name ?a .
?s foaf:surname ?b .
?s foaf:givenName ?c .
?s w3:label ?d .
?s purl:description ?e .
?s dbont:birthPlace ?f .
?s dbont:deathPlace ?g .
?s dbont:birthDate ?h .
?s dbont:deathDate ?i
}
WHERE {
?s foaf:name ?a .
?s foaf:surname ?b .
?s foaf:givenName ?c .
?s w3:label ?d .
?s purl:description ?e .
?s dbont:birthPlace ?f .
?s dbont:deathPlace ?g .
?s dbont:birthDate ?h .
?s dbont:deathDate ?i .
?s purl:description "Painter"@en
}
回答2:
As an alternative/shorthand, you can also use a DESCRIBE query, for example:
DESCRIBE <http://dbpedia.org/resource/Claude_Monet>
will give you a subgraph with all incoming and outgoing properties of <http://dbpedia.org/resource/Claude_Monet>
, or:
DESCRIBE ?x WHERE { ?x purl:description "Painter"@en }
will give you a subgraph for all ?x
that have a matching purl:description
.
Of course, this give you less control over the exact triples being extracted, but on the upside it's shorter and will also not need to be adapted if certain triple patterns do not exist for a particular resource (e.g. your CONSTRUCT query will not match if for any reason a particular painter does not have a deathDate
or foaf:surname
or any of the other properties in your WHERE clause).
A small caveat: the precise contents of the result of a DESCRIBE query are implementation-dependent. Some triplestores may choose to only return outgoing properties, for example. But Sesame (and, I believe, the DBPedia endpoint as well) return what is known as a Symmetric Concise Bounded Description.
回答3:
I see that you've already answered you own question, but I do want to point out that when the triples to be returned by a construct
query are exactly the same as those matched by the where
part, and the where
part doesn't contain anything too fancy, you can use the special shorthand construct where
:
16.2.4 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
FILTER
s and no complex graph patterns are allowed in the short form). The keyword WHERE is required in the short form.
Using a construct where
, as well as using the same namespaces that the DBpedia SPARQL endpoint already defines (which are conventional, e.g., rdfs
rather than w3
, and dc
rather than purl
), your query becomes:
construct where {
?s foaf:name ?a ;
foaf:surname ?b ;
foaf:givenName ?c ;
rdfs:label ?d ;
dc:description ?e, "Painter"@en ;
dbpedia-owl:birthPlace ?f ;
dbpedia-owl:deathPlace ?g ;
dbpedia-owl:birthDate ?h ;
dbpedia-owl:deathDate ?i .
}
SPARQL results
来源:https://stackoverflow.com/questions/19613074/sparql-construct-clause-to-include-predicate-literals