Question

I am trying to make a sparqle query for the capitols of all US's states. What is wrong with my sparqle query? It keeps failing.

# Find US states, their capitals and largest cities
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpr: <http://dbpedia.org/resource/>
PREFIX dbpo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>

#for all the capitols in the states
SELECT ?capitol
WHERE {
?capitol dbpo:state ?country .
{ ?capitol a dbpo:capitol } .
{ ?country a dbpprop:country "U.S." } .
}
Was it helpful?

Solution

Your query isn't legal. If you paste it into sparql.org's query validator, you'll see the syntax error:

Input:

  1 # Find US states, their capitals and largest cities
  2 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
  3 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  4 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  5 PREFIX dbpr: <http://dbpedia.org/resource/>
  6 PREFIX dbpo: <http://dbpedia.org/ontology/>
  7 PREFIX dbpprop: <http://dbpedia.org/property/>
  8 
  9 #for all the capitols in the states
 10 SELECT ?capitol
 11 WHERE {
 12 ?capitol dbpo:state ?country .
 13 { ?capitol a dbpo:capitol } .
 14 { ?country a dbpprop:country "U.S." } .
 15 }

Syntax error:

Encountered "  "\"U.S.\" "" at line 14, column 30.
Was expecting one of:
    "values" ...
    "graph" ...
    "optional" ...
    "minus" ...
    "bind" ...
    "service" ...
    "filter" ...
    "{" ...
    "}" ...
    ";" ...
    "," ...
    "." ...

It looks like you're using DBpedia, and if that's the case, then you can this a query like this at the DBpedia public endpoint. (The variable ?state is a bit of a misnomer; there are 56 results.)

select ?state ?capital where { 
  ?state dbpedia-owl:capital ?capital ;
         dbpedia-owl:country dbpedia:United_States .
}

SPARQL results

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top