Search code examples
sparqlwikidata-query-service

How to access child properties using Wikidata SPARQL Query Service?


I would like to access child properties of wikidata entities. An example may be property P1033, which is a child of P4952, for an entity such as Q49546. How can I do this dynamically in a SPARQL query?

Using the query builder provided by the online Wikidata Query Service, I can construct a simple query, which works for normal properties (in the linked example: mass), but not for the desired sub-properties (in the linked example: NPFA-code for health hazard), which end up empty, even though they are clearly set in the web-result. Side-note: it is a different example than the one from the first paragraph.

The desired objective is the dynamic query as follows:

SELECT ?p ?item ?itemDescription ?prop ?value ?valueLabel ?itemLabel ?itemAltLabel ?propLabel WHERE {
  BIND(wd:Q138809 AS ?item)
  ?prop wikibase:directClaim ?p.
  #?item ?p ?value.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
    ?value rdfs:label ?valueLabel.
    ?prop rdfs:label ?propLabel.
    ?item rdfs:label ?itemLabel;
      skos:altLabel ?itemAltLabel;
      schema:description ?itemDescription.
  }
}
ORDER BY DESC(?prop)
LIMIT 10

With the line 4 as a comment, I can get my propLabel as desired, but no value; doing it the other way round with the line not as comment, I do get only the properties, which are set on first level, but not the child properties.


Solution

  • Thanks to @AKSW, I herewith post the final query solving my problem:

    SELECT ?item ?itemLabel ?itemDescription ?itemAltLabel ?prop ?propertyLabel ?propertyValue ?propertyValueLabel ?qualifier ?qualifierLabel ?qualifierValue 
            { 
                VALUES (?item) {(wd:Q138809)}
                ?item ?prop ?statement . 
                ?statement ?ps ?propertyValue . 
                ?property wikibase:claim ?prop . 
                ?property wikibase:statementProperty ?ps . 
                OPTIONAL { ?statement ?pq ?qualifierValue . ?qualifier wikibase:qualifier ?pq . } 
                SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } 
    }
    

    The key step for me was to understand that child properties are actually called qualifiers.