Search code examples
sparqltaxonomywikidata

How to obtain the complete parent taxonomy of an item on Wikidata using SPARQL?


I'm trying to pull the complete taxonomy of an item in Wikidata. For example:

With Snowflake Inc (Q22078063) I want to get all the classes that Snowflake Inc is an instance of and then trace those classes up. I don't care about any subclasses of the classes that Snowflake Inc is an instance of, but I need the complete view of what the parent classes are.

Snowflake Inc (Q22078063)

I've tried several SPARQL queries, my most recent being this one:

SELECT DISTINCT ?Class ?ClassLabel ?linkTo ?linkToLabel {
  wd:Q22078063 wdt:P31/wdt:P279* ?Class.
  ?Class wdt:P279* ?linkTo.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }

I was attempting to use the subclass path to create a link between entities and ideally show a graph visual, but that does not work with this query.


Solution

  • The good thing is that the first part of your query will list all the classes in the hierarchy, i.e.

     SELECT DISTINCT ?Class ?ClassLabel {
      wd:Q22078063 wdt:P31/wdt:P279* ?Class.
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
        }
    

    The issue with the query is in the second part, i.e. ?Class wdt:P279* ?linkTo.. This will flatten the hierarchy instead of preserving its structure.

    A better query would be:

    SELECT DISTINCT ?Class1 ?Class1Label ?Class2 ?Class2Label {
          wd:Q22078063 wdt:P31/wdt:P279* ?Class1.
          ?Class1 wdt:P279 ?Class2
          SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
            }
    

    You could also replace the head of the query with a CONSTRUCT statement, i.e.

    CONSTRUCT {?Class1 wdt:P279 ?Class2}
    ....