I'm trying to query data from dbpedia by a country's name. I want it to find it whether there is a resource for that country or via its existence in wikiPageRedirects. Here is a working version:
PREFIX res: <http://dbpedia.org/resource/>
PREFIX ont: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?country ?capital ?label
WHERE {
{ res:Dominion_of_Canada ont:capital ?capital .
?capital rdfs:label ?label }
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country .
?country ont:capital ?capital .
?capital rdfs:label ?label }
FILTER (lang(?label) = "en")
}
I'd like (if possible), to factor out the ?country. Is it possible to assign a resource to a variable such that the SPARQL query looks like the following?
SELECT ?country ?capital ?label
WHERE {
{ ?country EXISTS res:Dominion_of_Canada } # to get the idea across
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country }
?country ont:capital ?capital .
?capital rdfs:label ?label .
FILTER (lang(?label) = "en")
}
As ever, speed is important, too. If the resource exists, then it'd be better if it skipped searching on wikiPageRedirects.