2
votes

I am a bit new to SPARQL and RDF and while I'm loving it, I'm stuck!

So, my app creates a thing with a label 'test6', a unique id and automatically it creates a default chapter associated with 'test6', with an id and a label 'Overview'. I want all the chapter labels associated with the thing who's label is 'test6' i.e 'Overview'

So the following triples exist (note, I had to trim the predicate because stackoverflow limits links in posts):

local:qUak8jXvdkw someOntology:label test6

local:qUak8jXvdkw someOntology:hasChapter qUak8jXvdkwOverview

local:qUak8jXvdkwOverview someOntology:label Overview

When I run this query:

PREFIX someOntology: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?id, ?chapterId, ?chapterLabel 
WHERE 
{ 
    ?id someOntology:label 'test6' . 
    ?id someOntology:hasChapter ?chapterId . 
    ?chapterId someOntology:label ?chapterLabel .  
} 

it returns an empty set.

I understand that I can use the Object of a triple as the Subject of another to join them is this correct?

If I use an OPTIONAL{} i.e:

PREFIX someOntology: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?id, ?chapterId, ?chapterLabel 
WHERE 
{ 
    ?id someOntology:label 'test6' . 
    ?id someOntology:hasChapter ?chapterId . 
    OPTIONAL{?chapterId someOntology:label ?chapterLabel} .  
}

it returns the following:

<[{
"id":{"type":"uri","value":"local:qUak8jXvdkw"},
"chapterId":{"type":"literal","value":"qUak8jXvdkwOverview"},
"chapterLabel":{}
}]>

I'm using 4store if that's any use.

Thanks in advance for any pointers..

1
Partially to the OP, partially to the community: I was aware that SO limits the number of links in posts for new users, but if this prevents those new users from asking legitimate questions that happen to contain URL-shaped URIs, this is a serious issue that should be brought up/discussed on Meta. - O. R. Mapper
Is it intentional that the object in the triple local:qUak8jXvdkw someOntology:hasChapter qUak8jXvdkwOverview is not prefixed with a namespace? - O. R. Mapper
Hi OR Mapper, thanks, I've tried using a namespace, i.e creating a triple with object = 'local:qUak8jXvdkwOverview' but it's giving me the same results, do I need to do something extra in the query, is my notation not correct or something? - RobertofRoberston

1 Answers

3
votes

Confusion between literals and URIs in the data.

The data has:

local:qUak8jXvdkw someOntology:hasChapter qUak8jXvdkwOverview
local:qUak8jXvdkwOverview someOntology:label Overview

(did the first line quoted above loose the "" on qUak8jXvdkwOverview - the results suggest it has)

This part

?id someOntology:hasChapter ?chapterId .

causes result:

"chapterId":{"type":"literal","value":"qUak8jXvdkwOverview"},

so ?chapterId here is literal "qUak8jXvdkwOverview".

But the query then has:

?chapterId someOntology:label ?chapterLabel . 

so ?chapterId here is URI local:qUak8jXvdkwOverview (literals can't be subjects anyway).