Rob H
Rob H

Reputation: 15010

How to write a SPARQL CONSTRUCT query that returns an RDF list?

Suppose I have the following RDF triples expressed in Turtle:

@prefix sorg: <http://www.schema.org/> .

<https://example.com/Foo> sorg:hasPart ( "item1" "item2" "item3" ) .

How do I write a SPARQL CONSTRUCT query that retrieves the list back out? If it's not possible, how can I write a SELECT query that will return the list elements in the correct order? I found this resource, but it does not seem to guarantee that elements will be returned in order.

Upvotes: 1

Views: 1731

Answers (2)

ahuh
ahuh

Reputation: 21

You should try this query to solve the problem:

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT {
    ?thing <urn:prop:to:list> ?list .
    ?listItems ?p ?o .
} WHERE {
    ?thing <urn:prop:to:list> ?list .
    ?list rdf:rest*/rdf:first? ?listItems .
    ?listItems ?p ?o .
}

Upvotes: 0

Brendan B
Brendan B

Reputation: 11

This will do the construct for you - linked onto original property to obtain the list so you can filter etc. as required (Just change ?thing and the property to your use case).

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT {
    ?thing <urn:prop:to:list> ?list .
    ?listRest rdf:first ?head ; 
       rdf:rest ?tail .
} WHERE {
    ?thing <urn:prop:to:list> ?list .

    ?list rdf:rest* ?listRest .
    ?listRest rdf:first ?head ;
       rdf:rest ?tail .
}

Upvotes: 1

Related Questions