2

I have the following RDF data (Person, worksAt, Branch) (Branch, location, Town) (Town, country, Country)

and the property path

worksAt, location, country

Can I formulate a SPARQL query where given a property e.g. country it will return me the most-left class (i.e. Person) of the graph?

1 Answers1

3

without property paths

SELECT ?person WHERE
{
  ?person :worksAt ?branch.
  ?branch :location ?town.
  ?town :country :?country.
}

with property paths

Using a sequence path defined as:

elt1 / elt2 A sequence path of elt1, followed by elt2

SELECT ?person WHERE
{
  ?person :worksAt/:location/:country ?country.
}

I don't understand what you mean with "given a property e.g. country", do you mean a specific instance instead? For example if the person should work in Germany, you would replace ?country with :Germany.

These examples assume that your properties only have a single class as a domain, e.g. only persons can work somewhere. Otherwise you have to add ?person a :Person. and so on.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • If I have no idea that ":worksAt" and ":location" properties precede the property of ":country", can I find them using a SPARQL query? If I find that these are all the previous properties I can also find that the root class of these properties is the class :Person. Thanks a lot. – George Perid Mar 23 '22 at 09:11
  • 1
    @GeorgePerid: According to the SPARQL 1.1 property path specification linked above, variables are not allowed in property paths. However workarounds are described in https://stackoverflow.com/a/26707541/398963. – Konrad Höffner Mar 23 '22 at 10:29