Rodrigo
Rodrigo

Reputation: 93

SPARQL query for specific information

I am struggling a lot to create some SPARQL queries. I need 3 specific things, and this is what i have so far:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>

select distinct ?title ?author ?country ?genre ?language
where {
  ?s rdf:type dbo:Book;
  dbp:title ?title;
  dbp:author ?author;
  dbp:country ?country;
  dbp:genre ?genre;
  dbp:language ?language.

}

This query will bring me a list of all books. What i really need is the ability to add some filters to this code. There are 3 things i want to filter by:

I've been struggling with this for too long and i simply cannot define these 3 queries. I am trying to implement a function that will execute a SPARQL statement using parameters passed by an user form. I found a few examples here and in the web but i just cannot build these 3 specific queries.

Upvotes: 1

Views: 898

Answers (1)

TallTed
TallTed

Reputation: 9434

As noted, not every book has every property, and some of your properties may not exist at all. For instance, I changed dbp:genre to dbo:literaryGenre, based on the description of Harry Potter and the Goblet of Fire. See query form, and results.

SELECT *
WHERE
 { ?s rdf:type dbo:Book .
   ?s rdfs:label ?bookLabel . 
      FILTER(LANGMATCHES(LANG(?bookLabel), 'en')) 
   ?s dbo:author ?author . 
   ?author rdfs:label ?authorLabel . 
      FILTER(LANGMATCHES(LANG(?authorLabel), 'en')) 
   ?authorLabel bif:contains "Rowling" 
   OPTIONAL { ?s dbp:country ?country .
              ?country rdfs:label ?countryLabel .
                 FILTER(LANGMATCHES(LANG(?countryLabel), 'en')) }
   OPTIONAL { ?s dbo:literaryGenre ?genre .
              ?genre rdfs:label ?genreLabel .
                 FILTER(LANGMATCHES(LANG(?genreLabel), 'en')) }
   OPTIONAL { ?s dbp:language ?language .
              ?language rdfs:label ?languageLabel .
                 FILTER(LANGMATCHES(LANG(?languageLabel), 'en')) }
}

Upvotes: 3

Related Questions