Reputation: 973
I have some data that contains a property hasNextSibling
. I would like to be able to query for an arbitrary set of the siblings. I can easily get all the siblings following a specific point, but how can I retrieve the siblings between X and Y.
For example, the data model is of the children of Old-Lady-in-the-Shoe. Some siblings in order are Fred, Brandy, Chris, Zack, Emma, Brad [and so on]. The list is much longer of course. I want the ability to say "give me all the siblings from Brandy to Emma" - returning Brandy, Chris, Zack, Emma.
I would have a query something like :
SELECT ?name WHERE {
?kid rdfs:label 'Brandy' .
?kid local:hasNextSibling+ ?sib .
?sib rdfs:label ?name .
// this is where I need to stop if ?name = 'Emma'
// but the graph still needs to complete and return all the other siblings
}
I can not simply filter for ?name
less than my ending name because the labels (names) are not in any determinable or natural order (hence the need for the hasNextSibling property to maintain the order).
Upvotes: 0
Views: 48
Reputation: 85913
Suppose you've got data like this, with kids with labels and a hasNextSibling property:
@prefix : <urn:ex:> .
:a :label 'a' ; :hasNextSibling :b .
:b :label 'b' ; :hasNextSibling :c .
:c :label 'c' ; :hasNextSibling :d .
:d :label 'd' ; :hasNextSibling :e .
:e :label 'e' ; :hasNextSibling :f .
:f :label 'f' ; :hasNextSibling :g .
:g :label 'g' .
Using property paths, you can search for kids that have a backward hasNextSibling chain to the first in the range that you want and a forward hasNextSibling chain to the last in the range that you want:
prefix : <urn:ex:>
select ?kid ?label where {
?kid ^:hasNextSibling* [:label 'b'] ;
:hasNextSibling* [:label 'd'] ;
:label ?label .
}
---------------
| kid | label |
===============
| :b | "b" |
| :c | "c" |
| :d | "d" |
---------------
If you want either of those to be exclusive ranges, you can use a +
property path instead of a *
property path.
prefix : <urn:ex:>
select ?kid ?label where {
?kid ^:hasNextSibling+ [:label 'b'] ;
:hasNextSibling+ [:label 'f'] ;
:label ?label .
}
---------------
| kid | label |
===============
| :c | "c" |
| :d | "d" |
| :e | "e" |
---------------
Upvotes: 1