SPARQL select query with condition? - sparql

I'm trying to form SPARQL query which will give Domain Names and Method Name against the given Java Class from below RDF. For e.g
Select DomainNames, MethodName where JavaClass = 'MyJavaClass'.
This is just a pseudo query. I need help in forming similar query in SPARQL. thanks.
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:DOL="http://www.MyOnt.com/something/v1#"
xmlns:DC="http://purl.org/dc/dcmitype/"
xmlns:foaf="http://xmlns.com/foaf/0.1/">
<rdf:Description rdf:about="http://www.MyOnt.com/something/data/MyJavaClass">
<DOL:belongsTo>
<rdf:Description rdf:about="http://www.MyOnt.com/something/data/MyDomain">
<DOL:domainName>MyDomainValue2</DOL:domainName>
<DOL:domainName>MyDomainValue</DOL:domainName>
</rdf:Description>
</DOL:belongsTo>
<DOL:hasMethod>
<rdf:Description rdf:about="http://www.MyOnt.com/something/data/MyMethod">
<DOL:returnType>MethodReturnType</DOL:returnType>
</rdf:Description>
</DOL:hasMethod>
<foaf:name>MyJavaClass</foaf:name>
</rdf:Description>
</rdf:RDF>

It's generally easier to understand what the SPARQL query should look like if you first put the data into Turtle, which has a syntax very similar to SPARQL. Here's what your data is in Turtle:
#prefix DOL: <http://www.MyOnt.com/something/v1#> .
#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/dcmitype/> .
<http://www.MyOnt.com/something/data/MyDomain>
DOL:domainName "MyDomainValue2" , "MyDomainValue" .
<http://www.MyOnt.com/something/data/MyJavaClass>
DOL:belongsTo <http://www.MyOnt.com/something/data/MyDomain> ;
DOL:hasMethod <http://www.MyOnt.com/something/data/MyMethod> ;
foaf:name "MyJavaClass" .
<http://www.MyOnt.com/something/data/MyMethod>
DOL:returnType "MethodReturnType" .
Once you've done that, the query looks almost exactly like the data, except with variables in it. The only catch here is that since you're looking for domains and methods, you need to use a union (assuming that you want to bind domains and methods as different variables).
prefix DOL: <http://www.MyOnt.com/something/v1#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select ?domain ?method {
?class foaf:name "MyJavaClass" .
{ ?class DOL:belongsTo ?domain }
union
{ ?class DOL:hasMethod ?method }
}
---------------------------------------------------------------------------------------------------
| domain | method |
===================================================================================================
| <http://www.MyOnt.com/something/data/MyDomain> | |
| | <http://www.MyOnt.com/something/data/MyMethod> |
---------------------------------------------------------------------------------------------------
If you're willing to have the domain and method bound to the same variable, you can use an alternation property path to select either a domain or a method:
prefix DOL: <http://www.MyOnt.com/something/v1#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select ?domainOrMethod {
?class foaf:name "MyJavaClass" ;
DOL:belongsTo|DOL:hasMethod ?domainOrMethod
}
--------------------------------------------------
| domainOrMethod |
==================================================
| <http://www.MyOnt.com/something/data/MyDomain> |
| <http://www.MyOnt.com/something/data/MyMethod> |
--------------------------------------------------
As another alternative, you could use a values block to specify the properties that you want to follow (hasMethod or belongsTo), in which case you can select that as well in order to know which type of value you have:
prefix DOL: <http://www.MyOnt.com/something/v1#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select ?property ?value {
values ?property { DOL:belongsTo DOL:hasMethod }
?class foaf:name "MyJavaClass" ;
?property ?value
}
------------------------------------------------------------------
| property | value |
==================================================================
| DOL:belongsTo | <http://www.MyOnt.com/something/data/MyDomain> |
| DOL:hasMethod | <http://www.MyOnt.com/something/data/MyMethod> |
------------------------------------------------------------------

Related

Results filtered out even though the data is there

Here's my query:
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 : <http://dbpedia.org/resource/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?resource ?parentOrSpouse
WHERE {
?resource a dbo:Royalty ;
rdfs:label ?label ;
dbo:parent ?parent ;
dbo:birthDate ?bd ;
dbo:birthPlace ?bp .
?bp dbo:isPartOf :England .
FILTER(?bd < '1900-01-01'^^xsd:dateTime) .
FILTER(?bd > '1800-01-01'^^xsd:dateTime) .
FILTER(LANGMATCHES(LANG(?label), 'en')) .
{ ?resource dbo:parent ?parentOrSpouse } UNION { ?resource dbo:spouse ?parentOrSpouse }
?parentOrSpouse dbo:birthPlace ?psbp .
?psbp dbo:isPartOf :England .
}
ORDER BY(?bd)
This searches for all royals born in England between 1800 and 1900 that have a spouse or parent that is born in England.
Result
In the result list I get
http://dbpedia.org/page/George_V with http://dbpedia.org/page/Mary_of_Teck listed as a spouse, but not Mary_of_Teck listed while George is clearly born in England.
Why is Mary disappearing? There are a lot of other people disappearing that should clearly be on the list when I look at the data.
So the solution to Mary not showing up is to use dbo:parent|dbo:spouse/dbo:wikiPageRedirects?, since George is refered by Mary via a redirect.
The other problem was dbo:birthPlace/dbo:location?/dbo:isPartOf dbr:England throwing an error that is probably(?) related to a bug in the compiler. Using ?parentOrSpouse dbo:birthPlace|dbo:birthPlace/dbo:location/dbo:isPartOf :England . instead seems to work fine.
Credit goes to #AKSW.

SPARQL for querying object property and its annotations

I am trying to learn about semantic web technology to see if it can replace the current Relational model in an application. I am using protege to design the ontology and Jena for the application which will be hosted on Apache TomEE.
I have created a simplified ontology to explain the query I am having. In people.owl (turtle below) there is only one class People. The object property hasChild links a person individual to his children who are also Person. Now I want to know the order of the children for a person.
For example, individual john has two children, with anne being first and jack being second. So to the hasChild object property of john I have added sequence annotation. The sequence is a number which tells the order in which the child was born for that person. So hasChild anne has sequence 1 and hasChild jack has sequnce 2. Similarly jane's hasChild jack has sequence 1.
The SPARQL I have to write is to get all the children of a given Person's name in the order they were born. So if I query children of "John Doe", I should get
|sequence | childname|
| 1 | Anne Doe |
| 2 | Jack Doe |
Below is the Ontology in turtle format.
#prefix : <http://www.semanticweb.org/abhishek/ontologies/people#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix people: <http://www.semanticweb.org/abhishek/ontologies/people#> .
#base <http://www.semanticweb.org/abhishek/ontologies/people> .
<http://www.semanticweb.org/abhishek/ontologies/people> rdf:type owl:Ontology .
### http://www.semanticweb.org/abhishek/ontologies/people#sequence
people:sequence rdf:type owl:AnnotationProperty .
### http://www.semanticweb.org/abhishek/ontologies/people#hasChild
people:hasChild rdf:type owl:ObjectProperty .
### http://www.semanticweb.org/abhishek/ontologies/people#hasParent
people:hasParent rdf:type owl:ObjectProperty .
### http://www.semanticweb.org/abhishek/ontologies/people#hasSpouse
people:hasSpouse rdf:type owl:ObjectProperty .
### http://www.semanticweb.org/abhishek/ontologies/people#name
people:name rdf:type owl:DatatypeProperty .
### http://www.semanticweb.org/abhishek/ontologies/people#Person
people:Person rdf:type owl:Class .
### http://www.semanticweb.org/abhishek/ontologies/people#anne
people:anne rdf:type owl:NamedIndividual ,
people:Person ;
people:name "Anne Doe"^^xsd:string .
### http://www.semanticweb.org/abhishek/ontologies/people#jack
people:jack rdf:type owl:NamedIndividual ,
people:Person ;
people:name "Jack Doe"^^xsd:string .
### http://www.semanticweb.org/abhishek/ontologies/people#jane
people:jane rdf:type owl:NamedIndividual ,
people:Person ;
people:hasChild people:jack ;
people:hasSpouse people:john ;
people:name "Jane Doe"^^xsd:string .
[ rdf:type owl:Axiom ;
owl:annotatedSource people:jane ;
owl:annotatedProperty people:hasChild ;
owl:annotatedTarget people:jack ;
people:sequence "1"^^xsd:int
] .
### http://www.semanticweb.org/abhishek/ontologies/people#john
people:john rdf:type owl:NamedIndividual ,
people:Person ;
people:hasChild people:anne ,
people:jack ;
people:hasSpouse people:jane ;
people:name "John Doe"^^xsd:string .
[ rdf:type owl:Axiom ;
owl:annotatedSource people:john ;
owl:annotatedProperty people:hasChild ;
owl:annotatedTarget people:anne ;
people:sequence "1"^^xsd:int
] .
[ rdf:type owl:Axiom ;
owl:annotatedSource people:john ;
owl:annotatedProperty people:hasChild ;
owl:annotatedTarget people:jack ;
people:sequence "2"^^xsd:int
] .
### Generated by the OWL API (version 4.2.6.20160910-2108) https://github.com/owlcs/owlapi
I have the query till now:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ppl: <http://www.semanticweb.org/abhishek/ontologies/people#>
SELECT ?sequence ?childname
WHERE { ?person rdf:type ppl:Person .
?person ppl:name ?name .
?person ppl:hasChild ?child .
#Get the ?sequence for the current child. sequence is annotated to hasChild.
?child ppl:name ?childname .
FILTER regex(?name, "John Doe") }
So I have two questions:
Should I change the ontology design to assign the sequence number to a child?
If the above approach is correct, how do I get the sequence for a child?
I posted the same question on Jena user list and got the recommendation to use RDF Collections. But from google search I found collections don't work with OWL (and hence Protege).
Thanks,
Abhishek
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ppl: <http://www.semanticweb.org/abhishek/ontologies/people#>
SELECT ?sequence ?childname
WHERE { ?person rdf:type ppl:Person .
?person ppl:name ?name .
?person ppl:hasChild ?child .
#Get the ?sequence for the current child. sequence is annotated to hasChild.
?child ppl:name ?childname .
?annotation owl:annotatedSource ?person ;
owl:annotatedTarget ?child ;
ppl:sequence ?sequence .
FILTER regex(?name, "John Doe") }

SPARQL query to get all class label with namespace prefix defined

I want to get all class that stored in sesame repository.
This is my query
SELECT ?class ?classLabel
WHERE {
?class rdf:type rdfs:Class.
?class rdfs:label ?classLabel.
}
It's return all URI of class with label. For example,
"http://example.com/A_Class" "A_Class"
"http://example.com/B_Class" "B_Class"
But, if namespace prefix already defined I want to get label with namespace defined. For example if I already defined namespace prefix "ex" for "http://example.com/", the result become
"http://example.com/A_Class" "ex:A_Class"
"http://example.com/B_Class" "ex:B_Class"
You want to add a URI prefix to the front of a label string?
I think you might be confused about what URI prefixes do. They're just shorthand for full URIs, and aren't part of the URI, and they don't have any bearing on strings.
You can do something like what you want with
SELECT ?class (CONCAT("ex:", ?classLabel) AS ?label
WHERE {
?class rdf:type rdfs:Class.
?class rdfs:label ?classLabel.
}
But the prefix won't depend on the prefix of the URI.
You could have a chain of IF()s that tests the starting characters of STR(?class), but it will get ugly quickly:
BIND(IF(STRSTARTS(STR(?class), "http://example.com/"), "ex:", "other:") as ?prefix)
then
SELECT ... (CONCAT(?prefix, ?classLabel) AS ?label
There's almost certainly an easier way to get what you want than doing it in SPARQL though :)
I agree with the other answers that say you're trying to do a strange thing, at least as it's stated in the question. Associating the prefix with the rdfs:label isn't all that useful, since the resulting string may or may not represent the actual URI of the class. Assuming that what you're trying to do is actually figure out a prefixed name for a URI given some set of prefix definitions, you can do this in SPARQL without needing lots of nested IFs; you can do it by using a VALUES and a FILTER.
Given data like this, in which a number of classes are defined (I don't define any labels here, since they're not necessary for this example),
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix ex1: <http://example1.com/> .
#prefix ex2: <http://example2.com/> .
#prefix ex3: <http://example3.com/> .
#prefix ex4: <http://example4.com/> .
ex1:A a rdfs:Class .
ex1:B1 a rdfs:Class .
ex2:A a rdfs:Class .
ex2:B2 a rdfs:Class .
ex3:A a rdfs:Class .
ex3:B3 a rdfs:Class .
ex3:A a rdfs:Class .
ex3:B4 a rdfs:Class .
ex4:A a rdfs:Class .
ex4:B4 a rdfs:Class .
you can write a query like this that selects all classes, checks whether their URI begins with one of a number of defined namespaces, and if so, returns the class and the prefixed form:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?class (group_concat(?prefixedName ; separator = "") as ?prefName) where {
values (?prefix ?ns) {
( "ex1:" <http://example1.com/> )
( "ex2:" <http://example2.com/> )
( "ex3:" <http://example3.com/> )
}
?class a rdfs:Class .
bind( if( strStarts( str(?class), str(?ns) ),
concat( ?prefix, strafter( str(?class), str(?ns) )),
"" )
as ?prefixedName )
}
group by ?class
order by ?class
which produces as results:
$ arq --data data.n3 --query query.sparql
---------------------------------------
| class | prefName |
=======================================
| <http://example1.com/A> | "ex1:A" |
| <http://example1.com/B1> | "ex1:B1" |
| <http://example2.com/A> | "ex2:A" |
| <http://example2.com/B2> | "ex2:B2" |
| <http://example3.com/A> | "ex3:A" |
| <http://example3.com/B3> | "ex3:B3" |
| <http://example3.com/B4> | "ex3:B4" |
| <http://example4.com/A> | "" |
| <http://example4.com/B4> | "" |
---------------------------------------

Use DBPedia to load all People along with some data

I am using DBpedia for the first time. I would like to download all the people in the dataset of people along with properties for commonName, nationality, birthDate, and knownFor (I will eventually stick it into an excel spread sheet using some sort of scripting language I think).
This was my first attempt at a query to do this job, however it does not work. I tried piecing it together from other bits of code I've seen on the internet. Does anyone know how to fix this? Thanks
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?person ?commonName ?nationality ?knownFor ? birthDate
WHERE {
?person a type:Person .
?person prop:commonName ?commonNameFilter(lang(?commonName) = 'en') .
?person prop:nationality ?nationality(lang(?nationality) = 'en') .
?person prop:knownFor ?knownFor(lang(?knownFor) = 'en') .
?person prop:birthDate ?birthDate .
}
EDIT: NEW VERSION OF CODE: RETURNS COMMONNAME (WITH NON-ENGLISH DUPLICATES). STILL MISSING OTHER PROPERTIES.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/ontology/>
SELECT DISTINCT * WHERE {
?person a dbpedia-owl:Person ;
dbpedia-owl:commonName ?commonName . FILTER(lang(?commonName) = 'en')
}
LIMIT 30
First, your query has a bunch of syntax issues:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
^ you probably want to use the dbpedia-owl properties which are
# in <http://dbpedia.org/ontology/>
SELECT ?person ?commonName ?nationality ?knownFor ? birthDate
^ space between ? and varname
WHERE {
?person a type:Person .
?person prop:commonName ?commonNameFilter(lang(?commonName) = 'en') .
^ This needs to be "?commonName . FILTER(..."
# and the same thing applies to your other
# filters
?person prop:nationality ?nationality(lang(?nationality) = 'en') .
?person prop:knownFor ?knownFor(lang(?knownFor) = 'en') .
?person prop:birthDate ?birthDate .
}
It's easier to build some of these queries incrementally, because then you can find out what properties some of the resources actually have, and then you can extend your query some more. The public endpoint has a number of predefined namespaces, and using those will make it easier for others to read your query. So, you can start by asking for people:
SELECT * WHERE {
?person a dbpedia-owl:Person .
}
LIMIT 10
SPARQL results
Seeing that that's working, you can look at some of the returned instances and see that they have dbpedia-owl:commonName properties, and then extend the query:
SELECT * WHERE {
?person a dbpedia-owl:Person ;
dbpedia-owl:commonName ?commonName .
}
LIMIT 10
SPARQL results
It's easy enough to extend this with the dbpedia-owl:birthDate property. I don't see a nationality predicate on the instances that I've looked at, so I'm not sure what you were basing the nationality query on. While I saw some use of the knownFor property, I didn't see it on many instances, so if you make it a required property, you're going to exclude lots of people. This sort of incremental approach will probably help you out in the long run, though.
Finding Properties
While the browseable ontology provides a nice way to find classes, I'm not sure whether there's such a nice way of finding properties. However, you can do something in a brute force manner. E.g., to find all the properties that have actually been used for Persons, you can run a query like the following. (Note: this query takes a while to execute, so if you use it, you should probably download the results.)
select distinct ?p where {
[] a dbpedia-owl:Person ;
?p [] .
}
SPARQL results
I'll note that dbpedia-owl:nationality does appear in that list.
To get all the properties for everything, you can download the ontology, and run a query like:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
select * where {
{ ?p a owl:ObjectProperty }
UNION
{ ?p a owl:DatatypeProperty }
}
I ran this locally using Jena's ARQ:
$ arq --query properties.sparql --data dbpedia_3.8.owl
----------------------------------------------------------------------------
| p |
============================================================================
| <http://dbpedia.org/ontology/regionServed> |
| <http://dbpedia.org/ontology/coachedTeam> |
| <http://dbpedia.org/ontology/legalForm> |
| <http://dbpedia.org/ontology/goldenCalfAward> |
| <http://dbpedia.org/ontology/composer> |
| <http://dbpedia.org/ontology/owningOrganisation> |
| <http://dbpedia.org/ontology/branchFrom> |
| <http://dbpedia.org/ontology/iso6393Code> |
...
| <http://dbpedia.org/ontology/classification> |
| <http://dbpedia.org/ontology/bgafdId> |
| <http://dbpedia.org/ontology/currencyCode> |
| <http://dbpedia.org/ontology/onChromosome> |
| <http://dbpedia.org/ontology/course> |
| <http://dbpedia.org/ontology/frequentlyUpdated> |
| <http://dbpedia.org/ontology/distance> |
| <http://dbpedia.org/ontology/volume> |
| <http://dbpedia.org/ontology/description> |
----------------------------------------------------------------------------
That won't provide the rdfs:domain and rdfs:range, but you could also ask for these, or for just those properties with rdfs:range dbpedia-owl:Person (but note that this won't get all the properties that could be used Person, since the range could be more or less specific):
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?p ?range where {
{ ?p a owl:ObjectProperty }
UNION
{ ?p a owl:DatatypeProperty }
?p rdfs:domain dbpedia-owl:Person ; rdfs:range ?range .
}
$ arq --query properties.sparql --data dbpedia_3.8.owl | head
--------------------------------------------------------------------------------------------------------
| p | range |
========================================================================================================
| dbpedia-owl:restingPlacePosition | <http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing> |
| dbpedia-owl:opponent | dbpedia-owl:Person |
| dbpedia-owl:employer | dbpedia-owl:Organisation |
| dbpedia-owl:hometown | dbpedia-owl:Settlement |
| dbpedia-owl:militaryBranch | dbpedia-owl:MilitaryUnit |
| dbpedia-owl:school | dbpedia-owl:EducationalInstitution |
| dbpedia-owl:ethnicity | dbpedia-owl:EthnicGroup |
...
| dbpedia-owl:sex | xsd:string |
| dbpedia-owl:hipSize | xsd:double |
| dbpedia-owl:individualisedPnd | xsd:nonNegativeInteger |
| dbpedia-owl:weddingParentsDate | xsd:date |
| dbpedia-owl:birthName | xsd:string |
| dbpedia-owl:networth | xsd:double |
| dbpedia-owl:birthYear | xsd:gYear |
| dbpedia-owl:bustSize | xsd:double |
| dbpedia-owl:description | xsd:string |
--------------------------------------------------------------------------------------------------------
I'm not very good at SPARQL yet, but i do see some syntax issues here. I rewrote the query to look like this:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?person, ?commonName, ?nationality, ?knownFor, ?birthDate
WHERE {
?person a type:Person .
?person prop:commonName ?commonName .
FILTER (lang(?commonName) = 'en') .
?person prop:nationality ?nationality .
FILTER (lang(?nationality) = 'en') .
?person prop:knownFor ?knownFor .
FILTER (lang(?knownFor) = 'en') .
?person prop:birthDate ?birthDate .
}
and now its at least running the query without an error. but i'm not seeing results. not sure why

How do build a subject search in SPARQL

I am trying to get all the books about the author William Shakespeare in the gutemberg project (http://www4.wiwiss.fu-berlin.de/gutendata/).
I can use this query in order to return all the items in which Shakespeare is the author :
PREFIX dc:<http://purl.org/dc/elements/1.1/>
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?bookTitle
WHERE {
?author foaf:name "Shakespeare, William, 1564-1616".
?book dc:creator ?author;
dc:title ?bookTitle
}
But I'd rather obtain the list of the criticism books about Shakespeare, using the dcterms:subjects
http://wifo5-04.informatik.uni-mannheim.de/gutendata/resource/subject/Shakespeare_William_1564-1616.
I would really appreciate your help !
I agree with RobV's comment that it's surprising that after writing the first query there would be much difficulty in writing the second. At any rate, the change is relatively trivial. I checked using both dc:subject and dcterms:subject, and it appears that the Gutenberg data is stored with dc:subject, so I've used that in this query, not dcterms:subject. This is the query; you just need to add dc:subject <.../Shakespeare...> to the pattern, and remove the author constraint. A few prefixes make the query easier to write, and the results easier to read, too.
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX gp: <http://wifo5-04.informatik.uni-mannheim.de/gutendata/resource/people/>
PREFIX gs: <http://wifo5-04.informatik.uni-mannheim.de/gutendata/resource/subject/>
PREFIX gr: <http://wifo5-04.informatik.uni-mannheim.de/gutendata/resource/>
SELECT ?book ?author ?bookTitle WHERE {
?book dc:creator ?author ;
dc:title ?bookTitle ;
dc:subject gs:Shakespeare_William_1564-1616 .
}
With a query like this, you should end up with results like:
--------------------------------------------------------------------------------------------
| book | author | bookTitle |
============================================================================================
| gr:etext14827 | gp:Guizot_François_Pierre_Guillaume_1787-1874 | "Étude sur Shakspeare" |
| gr:etext5429 | gp:Johnson_Samuel_1709-1784 | "Preface to Shakespeare" |
--------------------------------------------------------------------------------------------