Search code examples
databasesparqlwikidata

Using FILTER on property in SPARQL


How do I select all properties that start with a certain prefix, e.g. wdt:? I would like to get all properties related to an item, but am not interested in schema:, rdf:, p: and so on.

SELECT DISTINCT ?p
WHERE {
  wd:Q2 ?p ?entity.
}

This query would return all properties. I tried using FILTER(STRSTARTS(?p, "wdt:")), FILTER(STRSTARTS(str(?p), "wdt:")) and FILTER(STRSTARTS(xsd:string(?p), "wdt:")) to check if the property starts with wdt:, but the result is always empty.


Solution

  • wdt: is a prefix, meaning that it's a way to abbreviate something longer.

    wdt:P31 is actually stored as

    http://www.wikidata.org/prop/direct/P31
    

    You would in principle have a query where you specify a different prefix, like this:

    PREFIX hello: <http://www.wikidata.org/prop/direct/>

    And then you would have hello:P31 as your property.

    So try using this:

    SELECT DISTINCT ?p
    WHERE {
      wd:Q2 ?p ?entity.
    FILTER(STRSTARTS(STR(?p), "http://www.wikidata.org/prop/direct/"))
    

    }

    More about prefixes: https://en.wikibooks.org/wiki/SPARQL/Prefixes

    List with explanation of each prefix: https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Prefixes_used

    Full list of prefxes: https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Full_list_of_prefixes