Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rdf:collection in sparql

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 . 
}
like image 210
Magdalena Kaczmarek Avatar asked Apr 15 '12 12:04

Magdalena Kaczmarek


People also ask

How do I query using SPARQL?

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.

What types of queries does SPARQL support?

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.

What is prefix in SPARQL?

"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.

Is SPARQL similar to SQL?

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.


1 Answers

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.

like image 122
RobV Avatar answered Sep 26 '22 01:09

RobV