I have in an RDF file an rdf:collection
. When I have a collection of one author the following query returns nothing. However, the query works for two or more authors, but only returns two authors. What can I do to write out all authors?
<bibo:authorList rdf:parseType="Collection">
<rdf:Description rdf:about="http://openlibrary.org/authors/OL113143A"/>
<rdf:Description rdf:about="http://openlibrary.org/authors/OL6784959A"/>
</bibo:authorList>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/terms/>
select ?title ?author ?author2
where {
?x dc:title ?title .
?x bibo:authorList ?object.
?object rdf:first ?name.
?name rdf:value ?author.
?object rdf:rest ?object2.
?object2 rdf:first ?name2.
?name2 rdf:value ?author2 .
}
A SPARQL query may specify the dataset to be used for matching by using the FROM clause and the FROM NAMED clause to describe the RDF dataset. If a query provides such a dataset description, then it is used in place of any dataset that the query service would use if no dataset description is provided in a query.
SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.
"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.
SPARQL and SQL have very similar UNION and MINUS operators, which respectively add and remove solutions from a solution set. Because the datatypes of an SQL table are assumed to be uniform across all rows, care must be taken to align the datatypes of the SELECT.
Your problem is that you are trying to something semi-recursive so your results vary depending on the length of the RDF list. The query as written will only work for lists of length 2 or more, lists of length 1 won't work due to the fact the second portion of your query will have nothing to match.
If you want to access a collection the best way is with a property path like so (requires SPARQL 1.1):
SELECT * WHERE
{
?list rdf:rest*/rdf:first ?member .
}
You can adapt this general pattern to fit into your query as you see fit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With