SPARQL query doesn't deliver desired results - sparql

I've used the following code to list the band members of Punk Rock bands:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema>
SELECT distinct ?bandname ?bandmembername where {
?band
foaf:name ?bandname;
dbo:genre dbr:Punk_Rock.
?bandmember
dbo:bandMember ?bandmember;
rdfs:label ?bandmembername.
}
I would need the band members of any Punk_Rock band.

There is no syntax error.
There are 2 errors in your query.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema> should be PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
The dbo:bandMember ?bandmember predicate and object should be describing the ?band (which has members), not describing ?bandmember (which is the member)
With those fixes in the query, you should see the results you're hoping for --
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?bandname ?bandmembername
WHERE
{
?band
foaf:name ?bandname ;
dbo:bandMember ?bandmember ;
dbo:genre dbr:Punk_Rock .
?bandmember
rdfs:label ?bandmembername .
}

Related

How to access the value of the property from DBPedia using SPARQL

I wanted to access dbt:Cricket_by_country(value) inside of the dbp:wikiPageUsesTemplates (Property), but I am not able to access it. I am very new to the SPARQL
Below is my code
PREFIX cia: <http://www.semanticweb.org/neelc/ontologies/countries#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia-populated-place: <http://dbpedia.org/ontology/PopulatedPlace/>
PREFIX dbcat:<http://dbpedia.org/resource/Category>
select distinct ?name , ?sizeOfTeam, ?equipment, ?TeamCountry, ?Country
where
{
?cricket dct:subject <http://dbpedia.org/resource/Category:Cricket>.
?cricket dbp:name ?name.
?cricket dbo:teamSize ?sizeOfTeam.
?cricket dbo:equipment ?equipment.
?cricket dbp:wikiPageUsesTemplate ?TeamCountry .
?cricket dbp:TeamCountry.dbt:Cricket_by_country ?Country
FILTER (LANG(?name)="en")
FILTER regex(str(?cricket), "/resource/Cricket")
}

SPARQL-query to get data from wikidata not working

I try to get some image links from wikidata by running a SPARQL-query from my local Jena Fuseki instance. I want to merge it with data from my local graph. Unfortunately the query isn't delivering any data but runs and runs instead without any error message.
The sparql-query:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?image WHERE {
?s foaf:name ?name.
?s owl:sameAs ?wikidata_link.
FILTER regex(str(?wikidata_link), "wikidata").
SERVICE <https://query.wikidata.org/sparql> {
?wikidata_link wdt:P18 ?image.
}
} LIMIT 10
The test data I have in my local graph on the Jena Fuseki server:
#base <http://dmt.de/pages> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
#prefix dbp: <http://dbpedia.org/resource/> .
#prefix wd: <https://www.wikidata.org/entity/> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
<#john-cage>
a foaf:Person ;
foaf:name "John Cage";
owl:sameAs dbp:John_Cage, wd:Q180727.
<#karlheinz-stockhausen>
a foaf:Person ;
foaf:name "Karlheinz Stockhausen";
owl:sameAs dbp:Karlheinz_Stockhausen, wd:Q154556.
<#arnold-schoenberg>
a foaf:Person;
foaf:name "Arnold Schönberg";
owl:sameAs dbp:Arnold_Schoenberg, wd:Q154770.
I tried a similar query for dbpedia-data which run perfectly.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name ?dbpedia_link ?birthplace WHERE {
?s foaf:name ?name.
?s owl:sameAs ?dbpedia_link.
FILTER regex(str(?dbpedia_link),"dbpedia.org").
SERVICE <https://dbpedia.org/sparql> {
?dbpedia_link dbo:birthPlace ?birthplace.
}
} LIMIT 10
Any Ideas? Thanks in advance!

SPARQL Query to get a company by name if it contains text

I'm trying to get a company by name if it contains text
Example: honda motor co ltd
Maps to: https://www.wikidata.org/wiki/Q9584
I have this query that finds apple successfully
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
select distinct ?iri ?logo ?description {
?iri a dbpedia-owl:Company ;
dbpedia-owl:abstract ?description ;
rdfs:label ?lbl ;
foaf:depiction|dbpedia-owl:thumbnail ?logo .
?lbl bif:contains "'apple'"#en .
filter( langMatches(lang(?description),"en") )
}
Which returns results since it matches 'Apple Inc'
http://dbpedia.org/page/Apple_Inc.
But this query below doesn't match Accenture
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
select distinct ?iri ?logo ?description {
?iri a dbpedia-owl:Company ;
dbpedia-owl:abstract ?description ;
rdfs:label ?lbl ;
foaf:depiction|dbpedia-owl:thumbnail ?logo .
?lbl bif:contains "'accenture'"#en .
filter( langMatches(lang(?description),"en") )
}
I expect: http://dbpedia.org/page/Accenture
but get nothing
Not every resource in RDF must have every property! In your case, the logo doesn't exist which means you should make it an optional feature:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?iri ?logo ?description {
?iri a dbpedia-owl:Company ;
dbpedia-owl:abstract ?description ;
rdfs:label ?lbl .
?lbl bif:contains "'accenture'"#en .
FILTER( langMatches(lang(?description),"en") )
OPTIONAL {?iri foaf:depiction|dbpedia-owl:thumbnail ?logo }
}

How to query DBpedia SPARQL by resource uri?

I'm querying DBpedia types in SPARQL (http://dbpedia.org/sparql) by resource's label
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX : <http://dbpedia.org/resource/>
PREFIX ru: <http://ru.dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?type ?superType WHERE { {
?res rdfs:label "HarryPotter"#en.
} UNION {
?redir dbo:wikiPageRedirects ?res .
?redir rdfs:label "HarryPotter"#en .
}
?res rdf:type ?type .
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
It works fine.
But what if I know the exact resource - http://dbpedia.org/page/Harry_Potter? I tried something like:
?res a :Harry_Potter.
But it does not work.
How to query DBpedia types and supertypes if I know the resource URI? I can't figure out which property or operator I should use (e.g., rdfs:Resource, a, etc., which do not work)
When you write
?res a :Harry_Potter.
It doesn't work, because this means "a resource, which is of type :Harry_Potter". It is equivalent to
?res rdf:type :Harry_Potter.
:Harry_Potter identifies a resource and not the type, thus it should be used in place of ?res.
Also I think you mean Harry_Potter_(character), because that is the actual identifier and not redirect.
You query would be as simple as
SELECT ?type ?superType WHERE
{
# give me ?type of the resource
<http://dbpedia.org/resource/Harry_Potter_(character)> rdf:type ?type .
# give me ?superTypes of ?type
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
You can just put the URI as the subject in there WHERE conditions.
SELECT ?title, ?releaseDate
WHERE {
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbp:title ?title .
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbo:releaseDate ?releaseDate .
}

How to use the SPARQL CONSTRUCT in the new MeSH endpoint?

I'm trying to use the construct CONSTRUCT in the SPARQL MeSH endpoint but this one always return an empty query.
In particular i'm finding the alias of some medical names.
Basically the SELECT query that works is:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX meshv: <http://id.nlm.nih.gov/mesh/vocab#>
PREFIX mesh: <http://id.nlm.nih.gov/mesh/>
PREFIX mesh2015: <http://id.nlm.nih.gov/mesh/2015/>
SELECT ?d ?dName ?c ?cName
FROM <http://id.nlm.nih.gov/mesh>
WHERE {
?d a meshv:Descriptor .
?d meshv:concept ?c .
?d rdfs:label ?dName .
?c rdfs:label ?cName
FILTER(REGEX(?dName,'infection','i') || REGEX(?cName,'infection','i'))
}
ORDER BY ?d
and i'm finding something as the following:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX meshv: <http://id.nlm.nih.gov/mesh/vocab#>
PREFIX mesh: <http://id.nlm.nih.gov/mesh/>
PREFIX mesh2015: <http://id.nlm.nih.gov/mesh/2015/>
CONSTRUCT{
?dName a (subject)
is alias of (predicate)
?cName (object)
}
WHERE {
?d a meshv:Descriptor .
?d meshv:concept ?c .
?d rdfs:label ?dName .
?c rdfs:label ?cName
FILTER(REGEX(?dName,'infection','i') || REGEX(?cName,'infection','i'))
}
Reference documents
informs that CONSTRUCT is supported and you can try to query here, the SPARQL endpoint should be here.
Thank you for your help.