I am a beginner with SPARQL, and I am trying to deal with the endpoint of the Spanish National Library.
I have a code that works, here it is:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
?subjectLabel
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
But as some books has two or more subjects, the SPARQL repeats them in the results. I used group_concat, but for some reason it doesn't work:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
(GROUP_CONCAT(DISTINCT(?subjectLabel); separator="//") as ?subjects)
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
group by ?book
order by ?date
Does someone know where I am making a mistake?
Thanks!
Edit:
I was doing one thing wrong: as #AKSW said, I have to group all the variables at the end of the code, or add variables on the select. I have a reduced version of the code for testing this:
PREFIX bne: <http://datos.bne.es/def/>
PREFIX resource: <http://datos.bne.es/resource/>
SELECT ?book ?author ?title (GROUP_CONCAT(DISTINCT ?subject ; separator='//') AS ?subjects)
WHERE
{ ?book a bne:C1003 ;
bne:P3001 "Errata Naturae" ;
bne:P1011 ?author ;
bne:P3002 ?title ;
bne:OP3008 ?subject
}
#group everything here
GROUP BY ?book ?author ?title
#JeenBroekstra, when I run it in a SPARQL Validator, it says it is OK, but when I try to run it in the SPARQL endpoint of the library, it gives me an error:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'GROUP_CONCAT' before '('
As noted in comments, the problem is that the target endpoint is running on Virtuoso, Open Source Edition, v06.01.3127 as of 2011-11-16), which did not support GROUP_CONCAT in SPARQL, as SPARQL 1.1 had not yet been finalized.
Upgrading to a current version is strongly recommended!
There is a built-in function in this version of Virtuoso, available as sql:group_concat, as documented, which may serve you for now.
Related
Could anyone please help me with this code. It doesn't come out with any output but with no error.
q = """SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE {
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label.
?label dbo:abstract ?abstract.
?label dbo:director ?director.
?label dbo:starring ?starring.
FILTER (lang(?label) = "en")
FILTER (lang(?abstract) = "en")
}"""
from textwrap import wrap
for result in query(q):
print(result['label'],
"\n----Director----\n",result['director'],
"\n----Starring----\n",result['starring'],
"\n----Abstract----\n",
"\n".join(wrap(result['abstract'])))
<http://dbpedia.org/resource/Seven_Beauties> is the entity which has a Label, Abstract, Director, Star, etc.
The Label of that entity is a literal, and it has no Director, Star, Abstract, etc.
Try changing your query to --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties>
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:director ?director ;
dbo:starring ?starring
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
-- and see results from DBpedia.
DBpedia Live delivers nothing for the query above -- because the descriptions of films vary. Making some things optional will get you more results on DBpedia Live --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:abstract ?abstract }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:director ?director }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:starring ?starring }
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
I'm currently trying to teach myself how to formulate SPARQL queries to extract tourism-related information from DBpedia (via http://dbpedia.org/sparql/).
So far, I've managed to get all museums for a country.
select ?thing ?type ?category ?long ?lat ?country
where
{
VALUES ?country { <http://dbpedia.org/resource/Canada> }
optional
{
?city dbo:country ?country
}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type { dbo:Museum }
BIND( 'Museum' as ?category )
}
optional
{
?thing a ?type.
VALUES ?type { dbo:skiLift }
BIND( 'Skilift' as ?category )
}
optional
{
?thing geo:long ?long.
?thing geo:lat ?lat
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}
However, I don't understand what I need to do to also get the same information for things like dbo:skiLift, dbo:touristicSite and the like (found here: http://dbpedia.org/ontology/Place).
What am I doing wrong?
This is because both dbo:skiLift and dbo:touristicSite are properties. These resources show up in the page for Place not as subclasses of Place, but as properties which have the class Place as their domain or range. If you want to find subclasses of Place you can perform the exploratory query (which also uses property path to retrieve the transitive closure of the subClassOf property):
select ?thing
where
{
?thing rdfs:subClassOf+ <http://dbpedia.org/ontology/Place> .
}
Apart from that, I cannot understand why you use two optional clauses for different types in the same query. For example, the following query retrieves museums located at cities of Canada, possibly with their lat and lon, without the use of other optional clauses:
select ?thing ?city ?long ?lat
where
{
?city dbo:country <http://dbpedia.org/resource/Canada> .
?thing dbo:location ?city .
?thing a dbo:Museum .
optional
{
?thing geo:long ?long .
?thing geo:lat ?lat
}
}
Trying to execute some queries but when searching for foaf:name resultset is empty. Here's my code:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:name 'Cavallo domestico'#it .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'it') }
}
page exist http://it.dbpedia.org/resource/Equus_caballus/html
Apparently seems it's not related with languages different than english but with foaf:name request. If I execute following, retrieving generic foaf:givenName, it works:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:givenName 'Jimmy'#en .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'en') }
}
I think this wasn't working when I first mentioned that it didn't in a comment, but, as AKSW points out, this seems to be working now. The rdfs:label property has the article titles in various languages, not the foaf:name, so you can do this to get the types of Horse:
select ?x ?type {
?x a ?type ;
rdfs:label "Equus caballus"#it
}
SPARQL results
I have two SPARQL updates.First one:
INSERT
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH <[http://example/bookStore]>
{ ?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
Second:
INSERT
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH <[http://example/bookStore3]>
{ ?book dc:date ?date .
FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
Can i combine them with the UNION operator? And if yes, is it an equivalent result? Is it possible to use UNION in SPARQL updates such as in "Select"?
AndyS's answer is correct; you can combine them, and the description of UNION is found in section 7 Matching Alternatives of the SPARQL specification. The combined query would be:
INSERT {
GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{
{
GRAPH <[http://example/bookStore]> {
?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
}
}
UNION
{
GRAPH <[http://example/bookStore3]> {
?book dc:date ?date .
FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
}
}
}
In this particular case where the patterns are so similar, you could also just abstract out the differing parts with VALUES:
INSERT {
GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{
values (?graph ?startDate) {
(<[http://example/bookStore]> "1970-01-01T00:00:00-02:00"^^xsd:dateTime)
(<[http://example/bookStore3]> "1980-01-01T00:00:00-02:00"^^xsd:dateTime)
}
GRAPH ?graph {
?book dc:date ?date .
FILTER ( ?date > ?startDate )
?book ?p ?v
}
}
The WHERE clause is the same as SPARQL Query - you can use UNION.
I want to get triples about cities, which are from certain country. How can I do that?
I tried:
CONSTRUCT { ?c rdfs:label ?name . ?c rdfs:comment ?desc }
WHERE {
?c dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> .
?c rdfs:label ?name .
?c rdfs:comment ?desc .
?c <http://dbpedia.org/ontology/country> ?country . ?country a <http://dbpedia.org/resource/CountryName>
FILTER ( lang(?name) = "en" && lang(?desc) = "en" )
}
but no luck :/ how can i do this?
CONSTRUCT { ?c rdfs:label ?name }
WHERE {
?c dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> .
?c rdfs:label ?name .
?c dbpedia-owl:country <http://dbpedia.org/resource/Country> .
OPTIONAL { ?c dbpedia-owl:areaCode ?areacode }
FILTER ( lang(?name) = "pl" && ?population > 5000)
}
Hope it will help :)