How to form SPARQL queries that refers to multiple

2019-06-06 02:37发布

问题:

My question is a followup with my first question about SPARQL here.

My SPARQL query results for Mountain objects are here.

From those results I picked a certain object resource. Now I want to get values of "is dbpedia-owl:highestPlace of" records for this chosen Mountain object.

That is, names of mountain ranges for which this mountain is highest place of.

This is, as I figure, complex. Not only because I do not know the required syntax, but also I get two objects here.

  • One of them is Mont Blank Massif which is of type "place".
  • Another one is Western Alps which is of type "mountain range" - my desired record.

I need record # 2 above but not 1. I know 1 is also relevant but sometimes it doesn't follow same pattern. Sometimes the records appear to be of YAGO type, which can be totally misleading. To be safe, I simply want to discard those records whenever there is type mismatch.

How can I form my SPARQL query to get these "is dbpedia-owl:highestPlace of" records and also have the type filtering?

回答1:

you can use this query, note however that Mont_Blanc_massif in your example is both a dbpedia-owl:Place and a dbpedia-owl:MountainRange

select * where {
   ?place dbpedia-owl:highestPlace :Mont_Blanc.
   ?place rdf:type dbpedia-owl:MountainRange.
}

run query

edit after comment: filter
It is not really clear what you want to filter (yago?), technically you can filter for example like this:

select * where {
   ?place dbpedia-owl:highestPlace :Mont_Blanc.
   ?place rdf:type dbpedia-owl:MountainRange.
   FILTER NOT EXISTS { 
     ?place ?pred ?obj
     Filter (regex(?obj, "yago"))
   }
}

this filters out results that have any object with 'yago' in its URL.



回答2:

Extending the result from the previous answer, the appropriate query would be

select * where {
  ?mountain a dbpedia-owl:Mountain ;
            dbpedia-owl:abstract ?abstract ;
            foaf:depiction ?depiction .
  ?range a dbpedia-owl:MountainRange ;
         dbpedia-owl:highestPlace ?mountain .
  FILTER(langMatches(lang(?abstract),"EN"))
}
LIMIT 10

SPARQL Results

This selects mountains with English abstracts that have at least one depiction (or else the pattern wouldn't match) and for which there is some mountain range of which the mountain is the highest place. Without the parts from the earlier question, if you just want to retrieve mountains that are the highest place of a range, you can use a query like this:

select * where {
  ?mountain a dbpedia-owl:Mountain .
  ?range a dbpedia-owl:MountainRange ;
         dbpedia-owl:highestPlace ?mountain .
}
LIMIT 10

SPARQL results