问题
I need to get all players who have ever played for football team using SPARQL query and dbpedia.org
I can get current team members using http://dbpedia.org/sparql and this query:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX p: <http://dbpedia.org/property/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT * WHERE {
<http://dbpedia.org/resource/Manchester_United_F.C.> p:name ?player.
?player dbpedia-owl:birthPlace ?city;
dbpedia-owl:birthDate ?dob.
}
How to get all players who have ever played for this football team ?
回答1:
I'd use a query like this:
select distinct ?player ?birthPlace ?birthDate where {
?player dbpedia-owl:team <http://dbpedia.org/resource/Manchester_United_F.C.> ;
a dbpedia-owl:Person ;
dbpedia-owl:wikiPageID [] .
optional { ?player dbpedia-owl:birthPlace ?birthPlace }
optional { ?player dbpedia-owl:birthDate ?birthDate }
}
SPARQL results
Using optional ensures that players without listed birthplaces or birthdates can still be included in the results. Using a dbpedia-owl:Person helps remove some "dirty" data (i.e., things that have a dbpedia-owl:team property, but that aren't players). Similarly, adding dbpedia-owl:wikiPageID [] ensures that the results are things that exist as articles. If you don't include that, then you get results like Manchester_United_F.C.__Ryan_Giggs__1, which appears to be a sort of career station entity. (Note that Ryan Giggs has, e.g., dbpedia-owl:careerStation dbpedia:Ryan_Giggs__1.)
来源:https://stackoverflow.com/questions/27721834/how-to-build-correct-sparql-query