Search code examples
sparqlwikipediadbpedia

How to traverse from a Wikipedia Parent category to all its subcategories


I am trying to collect the list of landmarks listed in Wikipedia per country, for example US Landmarks . But this parent Category just includes subcategories and It is not clear how many layers one has to go down to get to the actual data. Is there any way to make a SPARQL query for something like that? Or is there any other way to do it?
Simple queries like this returns nothing

SELECT DISTINCT ?namedEnt WHERE {?namedEnt <http://purl.org/dc/terms/subject>  <http://dbpedia.org/resource/Category:Category:Landmarks_in_the_United_States_by_state>} limit 10

Solution

  • It seems relationship between sucategory and its parent category is represented using skos:broader in DBpedia. If you combine that with *, which represents one or more applications of a predicate, the query becomes:

    PREFIX cat: <http://dbpedia.org/resource/Category:>
    PREFIX dcterms: <http://purl.org/dc/terms/>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    
    SELECT DISTINCT ?page
    WHERE {
        ?subcat skos:broader* cat:Landmarks_in_the_United_States_by_state.
        ?page dcterms:subject ?subcat
    }
    

    Or you can shorten it using / to just:

    SELECT DISTINCT ?page
    WHERE {
        ?page dcterms:subject/skos:broader* cat:Landmarks_in_the_United_States_by_state
    }