2

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.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
  • 2
    Not sure what exactly you're asking, but what you call "child properties" is known as statement qualifiers in the Wikidata context. Not sure what exactly you mean by "dynamic", but getting all qualifiers for a proeprty would be `SELECT ?item ?itemDescription ?itemLabel ?itemAltLabel ?qualifierProperty ?value WHERE { BIND(wd:Q138809 AS ?item) SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } ?item p:P4952 ?stmt. ?stmt ps:P4952 ?safety. ?stmt ?qualifierProperty ?value } LIMIT 50` – UninformedUser Sep 01 '19 at 10:25
  • Thanks for the comment, sorry that the question was unclear. By "child properties" I may indeed mean qualifiers. By "dynamic" I mean that I can select them without knowing their _id_, just as the – in the question posted – code does for the "normal" properties. – BernhardWebstudio Sep 01 '19 at 11:19
  • 1
    The query in my comment returns all qualifiers without knowing them. Is that not what you want? – UninformedUser Sep 01 '19 at 11:22
  • It requires the knowledge of p:P4952, but yes, it goes in the right direction, thanks! I guess I will be able to merge yours with mine to find the desired answer, or you may so I could accept it as an answer. Thank you for the help! – BernhardWebstudio Sep 01 '19 at 11:30
  • 2
    ah, now I get it - you basically want to have it for all properties of an entity. And you also want the labels of the properties which makes it a bit more complex. This could work: `SELECT ?propertyLabel ?propertyValueLabel ?qualifierLabel ?qualifierValueLabel { VALUES (?item) {(wd:Q138809)} ?item ?p ?statement . ?statement ?ps ?propertyValue . ?property wikibase:claim ?p. ?property wikibase:statementProperty ?ps. OPTIONAL { ?statement ?pq ?qualifierValue . ?qualifier wikibase:qualifier ?pq .} SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } }` – UninformedUser Sep 01 '19 at 13:27
  • Exactly, thank you very much. Do you want to turn your comment into an answer so I can accept it? – BernhardWebstudio Sep 01 '19 at 14:12

1 Answers1

3

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.