Search code examples
sparqldbpedia

Selecting variable in form of "is dbp:variable of" in SPARQL


I am trying to select what is some country (for example India) northeast of from DBPedia using SPARQL. I am using query:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT  ?northeast ?ne
WHERE {
    OPTIONAL {<http://dbpedia.org/resource/India> dbp:northeast ?northeast }
.   OPTIONAL {?northeast foaf:name ?ne}
}

where in variable dbp:northeast should be

Comilla_Division, Rangpur_Division

but it selects long list of other things. I figured problem might be that in DBPedia is that variable in this syntax: "is dbp:northeast of".

Does someone know how to formulate query for that?

Thanks!


Solution

  • As for "is ... of", see this answer. Your query should be:

    SELECT ?northeast ?ne WHERE {
        ?northeast dbp:northeast dbr:India 
        OPTIONAL {?northeast foaf:name ?ne}
    }
    

    Try it!

    or

    SELECT ?northeast ?ne WHERE {
        VALUES (?country) {(dbr:India)}
        ?country ^dbp:northeast ?northeast; rdfs:label ?ne.
        FILTER (lang(?ne) = "en") .
    }
    

    Try it!


    It is much more interesting, why "long list of other things" is returned instead of empty resultset.

    From In SPARQL, Order Matters:

    SPARQL evaluation starts out over the so-called “empty mapping” (also called “universal mapping”): [if] the OPTIONAL block is not able to retrieve any results, and given the semantics of OPTIONAL, this step simply retains the empty mapping.

    In general, never start with OPTIONAL.