Yahya
Yahya

Reputation: 11

Right syntax SPARQL query from dbpedia.org

I am new to RDF and SPARQL and am struggling with what syntax is correct. When I look up examples on the web, it looks fairly easy, but when I try to apply it and do exercises, it simply doesn't work.

Currently I have to make a list with three columns, containing the European Union countries, population size (ranked from big to small), and the type of government the country has.

I have tried a number of PREFIX's and practically every imaginable text (See below), but nothing works.

PREFIX dbc: <dbc:Member_states_of_the_European_Union>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX schema: <http://schema.org/Country>

SELECT ?populationsize
WHERE
{
?populationsize dbo:populationTotal ?number
}

Upvotes: 0

Views: 405

Answers (2)

TallTed
TallTed

Reputation: 9434

Note that your revised query doesn't use, and so you don't need to include, the yago: nor dct: prefixes. I've also added another prefix, for the Category.

PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dct:  <http://purl.org/dc/terms/>

SELECT DISTINCT  *
WHERE
{ ?country  dct:subject          dbc:Member_states_of_the_European_Union ;
            dbo:populationTotal  ?populationTotal ;
            dbo:governmentType   ?governmentType
}
ORDER BY DESC(?populationTotal)

That's the same query as this, with no PREFIX at all --

SELECT DISTINCT  *
WHERE
{ ?country  http://purl.org/dc/terms/subject             http://dbpedia.org/resource/Category:Member_states_of_the_European_Union ;
            http://dbpedia.org/ontology/populationTotal  ?populationTotal ;
            http://dbpedia.org/ontology/governmentType   ?governmentType
}
ORDER BY DESC(?populationTotal)

The labels you want are just an attribute of the URIs currently being retrieved. Labels exist in multiple languages, so I've added a FILTER here to get only the labels tagged as English.

PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dct:  <http://purl.org/dc/terms/>

SELECT DISTINCT  *
WHERE
{ ?country  dct:subject          dbc:Member_states_of_the_European_Union ;
            dbo:populationTotal  ?populationTotal ;
            dbo:governmentType   ?governmentType .
  OPTIONAL  { ?country         rdfs:label  ?countryLabel  .
              FILTER ( lang(?countryLabel)="en" ) }
  OPTIONAL  { ?governmentType  rdfs:label  ?governmentTypeLabel  .
              FILTER ( lang(?governmentTypeLabel)="en" ) }
}
ORDER BY DESC(?populationTotal)

I know you don't want the literal types nor langtags, so here's one more tweak to make all the output simple --

PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dct:  <http://purl.org/dc/terms/>

SELECT DISTINCT  ( STR(?populationTotal)     AS ?Population )
                 ( STR(?countryLabel)        AS ?Country )
                 ( STR(?governmentTypeLabel) AS ?Government )
WHERE
{ ?country  dct:subject          dbc:Member_states_of_the_European_Union ;
            dbo:populationTotal  ?populationTotal ;
            dbo:governmentType   ?governmentType .
  OPTIONAL  { ?country  rdfs:label  ?countryLabel  .
                FILTER ( LANG(?countryLabel) = "en" ) }
  OPTIONAL  { ?governmentType  rdfs:label  ?governmentTypeLabel  .
                FILTER ( LANG(?governmentTypeLabel) = "en" ) }
}
ORDER BY DESC(?populationTotal)

Upvotes: 1

Yahya
Yahya

Reputation: 11

Personally, this is still very unclear to me, but this works!

PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  dct:  <http://purl.org/dc/terms/>

SELECT DISTINCT  *
WHERE
{ ?country  dct:subject  
<http://dbpedia.org/resource/Category:Member_states_of_the_European_Union> ;
          dbo:populationTotal  ?populationTotal;
          dbo:governmentType  ?governmentType
}

ORDER BY DESC(?populationTotal)

Upvotes: 1

Related Questions