Search code examples
joinrdfsparql

Sparql join issue


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


Solution

  • 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).