简体   繁体   中英

Sparql: How to retrieve an arbitrary number of values of the same property?

Given a subject with multiple occurrences of the same property, is it possible to retrieve all the objects/values they point to, without specifying in advance their number?

This question resembles this one .

Example data (taken from link above):

@prefix example: <http://example.org/> .
example:subject1 example:property example:object1 .
example:subject1 example:property example:object2 .
example:subject1 example:property example:object3 .
...

It is possible to get a defined number of objects via:

select ?subject ?object1/2/n 
where {?subject example:property ?object1, ?object2 .}
filter( ?object1 != ?object2 ).

But how to do if we do not know the number of occurrences of the property in advance? (besides programmatically). Thank you.

I suppose, you are a bit confused by the Turtle syntax .

In fact,

SELECT ?object1
   WHERE {
   ?subject example:property ?object1, ?object2 .
   FILTER ( ?object1 != ?object2 ).
   }

is just a syntactic sugar for:

SELECT ?object1
   WHERE {
   ?subject example:property ?object1 .
   ?subject example:property ?object2 .
   FILTER ( ?object1 != ?object2 ).
   }

This pattern matches all the triples which are different from each other only by their objects.
I suppose, "a subject with multiple occurrences of the same property" (from your question) means "there exist different objects for the same subject and the same property".

The pattern is symmetric, ie if :object1 is bound to ?object1 in a result, then :object1 will be bound to ?object2 in another result. It doesn't matter for this pattern whether any third triple with different object :object3 exists, but if it exists, :object3 will be bound to ?object1 in yet another result.

You can try also something like this for better understanding:

SELECT DISTINCT ?object1
   WHERE {
   ?subject example:property ?object1 .
   FILTER EXISTS {
      ?subject example:property ?object2 .
      FILTER ( ?object1 != ?object2 )
      }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM