Search code examples
sparqlcategoriesmemberwikidatawikimedia

SPARQL Wikidata - Retrieve members of Wikimedia Category


I want to retrieve the members of the Wikimedia Category American rock singers. I must use Wikidata and it tells me to use P31 with Q4167836 . The query below only returns information about the Category page and not about its members.

Is there any other way to retrieve the members of the Wikimedia Category? I also tried using the term dcterms:subject, which I use successfully with DBpedia, but in Wikidata the query returns empty results.

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
prefix wikibase: <http://wikiba.se/ontology#>
prefix bd: <http://www.bigdata.com/rdf#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT DISTINCT ?subject ?predicate ?object ?objectLabel WHERE {
     ?subject ?predicate ?object ; wdt:P31 wd:Q4167836 ; 
     rdfs:label "Category:American rock singers"@en 
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}

Try here


Solution

  • Try this query on Wikidata:

    SELECT ?subjectLabel WHERE {
        ?subject wdt:P27 wd:Q30;
                 wdt:P136 wd:Q11399 .
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
    }
    

    Or try this query on DBpedia (as Mark Miller linked to):

    SELECT str(?label) WHERE {
      ?subject dct:subject dbc:American_rock_singers ;
               rdfs:label ?label .
      FILTER (lang(?label) = "en")
    }
    

    Or if you must use Wikidata, try this federated query on Wikidata:

    PREFIX dbc: <http://dbpedia.org/resource/Category:>
    PREFIX dct: <http://purl.org/dc/terms/>
    SELECT ?wsubjectLabel WHERE {
        SERVICE <http://dbpedia.org/sparql> {
             ?subject dct:subject dbc:American_rock_singers .
             ?subject owl:sameAs ?wsubject .
             FILTER (STRSTARTS(STR(?wsubject), "http://www.wikidata.org"))
        }
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }