Michael DeBellis
Michael DeBellis

Reputation: 129

How To Unpack SPARQL Resultset Into Triples in DBpedia Query

I have a DBpedia query I'm using on Virtuoso like this:

SELECT ?state ?stateLabel ?population 
WHERE {
     ?state rdf:type dbo:AdministrativeRegion ;
            rdfs:label ?stateLabel ;
            dbo:country dbr:India ;
            dbo:populationTotal ?population.
     FILTER (lang(?stateLabel) = "en")}
ORDER BY ?stateLabel

The actual query is much longer and I've omitted the prefixes. I want to get triples, for example:

dbr:Dharwad_district dbo:populationTotal 1847023

that I can import into an OWL ontology. The results I get are in a results set. I can't figure out how to extract the triples from the result set data structure. For example, in Turtle, the first lines of the results are:

@prefix res: <http://www.w3.org/2005/sparql-results#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
_:_ a res:ResultSet .
_:_ res:resultVariable "state" , "stateLabel" , "abs" , "latitude" , "longitude" , "population" , "city" , "cityLabel" .
@prefix dbr:    <http://dbpedia.org/resource/> .
@prefix xsd:    <http://www.w3.org/2001/XMLSchema#> .
_:_ res:solution [
      res:binding [ res:variable "state" ; res:value dbr:Aalo ] ;
      res:binding [ res:variable "stateLabel" ; res:value "Aalo"@en ] ;...

Upvotes: 1

Views: 61

Answers (1)

An alternative (to extracting the relevant triples from the res:ResultSet) could be to use a SPARQL CONSTRUCT query.

You can keep the WHERE clause, and instead of the SELECT clause, you add a CONSTRUCT clause which describes the triples you want to produce.

CONSTRUCT {
  ?state dbo:populationTotal ?population .
} 
WHERE {
  ?state 
    rdf:type dbo:AdministrativeRegion ;
    rdfs:label ?stateLabel ;
    dbo:country dbr:India ;
    dbo:populationTotal ?population .
  FILTER (lang(?stateLabel) = "en")
}
ORDER BY ?stateLabel

Upvotes: 0

Related Questions