Question

I have the following SPARQL query:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX type: <http://dbpedia.org/class/yago/>      
PREFIX prop: <http://dbpedia.org/property/>
SELECT *
WHERE {
  ?person a foaf:Person;
  foaf:name ?name;
  prop:deathCause ?death_cause.
  FILTER (langMatches(lang(?name), "EN")) .
}
LIMIT 50

If you run this here: http://dbpedia.org/snorql/

You will see that you get a lot of results. Now I would like to filter out one death cause, let's say 'Traffic collision'. So this should be simply by adding a filter:

FILTER (?death_cause = "Traffic collision").

So the query should then be:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX type: <http://dbpedia.org/class/yago/>      
PREFIX prop: <http://dbpedia.org/property/>
SELECT *
WHERE {
  ?person a foaf:Person;
  foaf:name ?name;
  prop:deathCause ?death_cause.
  FILTER (?death_cause = "Traffic collision").
  FILTER (langMatches(lang(?name), "EN")) .
}
LIMIT 50

However this returns nothing. Anyone that knows what is wrong with the query? Thanks.

Was it helpful?

Solution

You will see that you get a lot of results. Now I would like to filter out one death cause, let's say 'Traffic collision'. So this should be simply by adding a filter:

FILTER (?death_cause = "Traffic collision" ).

Filters define what things you keep, not what things you remove, so if you wanted to filter out traffic collisions, you'd actually want:

FILTER ( ?death_cause != "Traffic collision" )

If you try that, though, you'll still see traffic collisions in your results, because there's a difference between "Traffic collision" and "Traffic collision"@en. The former (which is what your code would be removing) is a plain literal (i.e., without a datatype or a language tag). The latter is a literal with the language tag "en". These are not the same, so filtering out one doesn't filter out the other, and keeping one won't keep the other. To remove the "Traffic collision"@en, you can filter it out with:

FILTER ( ?death_cause != "Traffic collision"@en )

Alternatively, you can use the str function to get the lexical part of a literal, so you could filter out "Traffic collision" regardless of the language tag (or datatype, if it appeared as a typed literal):

FILTER ( str(?death_cause) != "Traffic collision" )

Thus:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX type: <http://dbpedia.org/class/yago/>      
PREFIX prop: <http://dbpedia.org/property/>
SELECT *
WHERE {
  ?person a foaf:Person;
  foaf:name ?name;
  prop:deathCause ?death_cause.
  FILTER (langMatches(lang(?name), "EN")) .
  FILTER ( ?death_cause != "Traffic collision"@en )
}
LIMIT 50

SPARQL results

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