sparql查询所有美国州的首都

时间:2014-03-31 03:33:46

标签: sparql

我正在尝试对所有美国州的国会大厦进行sparql查询。我的sparql查询有什么问题?它一直都在失败。

# 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." } .
}

1 个答案:

答案 0 :(得分:3)

您的查询不合法。如果您将其粘贴到sparql.org's query validator,则会看到语法错误:

  

输入:

  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 }
     

语法错误:

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

您似乎正在使用DBpedia,如果是这样的话,那么您可以在DBpedia public endpoint进行类似的查询。 (变量?state有点用词不当;有56个结果。)

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

SPARQL results