sparqlsemantic-webwikidatalinked-data

Wikidata SPARL query by generic property return object instead of statement


I want to disseminate the query by spreading the nodes like in the below query.

SELECT ?item ?itemLabel  ?prop ?object ?objectLabel ?prop2 ?object2
WHERE 
{
  VALUES (?item) { (wd:Q12418)}
  ?item ?prop ?object.
   ?object ?prop2 ?object2
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language
}

Here I start with wd:Q12418(Mona Lisa) and I retrieve all properties of it - than for each object I want to retrieve all properties of it as well.

The problem is that ?item ?prop ?object line returns statement for object rather than the object itself. So it's pointing to Mona Lisa's statement like this: https://www.wikidata.org/wiki/Q12418#Q12418$9c08a93a-4591-561d-36a9-78083ae0a3fa

In this case second part (?object ?prop2 ?object2) works on this statement so it returns something if only that statement has subproperties. What I'd like to get is the object(e.g. painter Leonardo Da Vinci) rather than the statement so that I can iterate through object's properties.

How can I achieve that?


Solution

  • You want ?prop to start with the wdt: prefix, then you can add this filter function:

    filter(strstarts(str(?prop), str(wdt:)))
    

    and, if you need, an analogous one for ?prop2.