Search code examples
sparqlreturn-type

Type of returned value in SPARQL Query


Is it possible to know the type of the return values in a SPARQL query? For example, is there a function to define the type of ?x ?price ?p in the following query?

SELECT DISTINCT ?x ?price ?p
                    WHERE {
                        ?x a :Product .
                        ?x :price ?price .
                        ?x ?p ?o .
                    }

I want to know that

typeOf(x) = resource
typeOf(?p) = property
typeOf(?price) = property target etc.

Solution

  • datatype(?x)

    The datatype function will tell you whether a result is a resource or a literal and in the latter case tell you which datatype it has exactly.

    For example, the following query on https://dbpedia.org/sparql...

    SELECT DISTINCT ?x ?code ?p datatype(?x) datatype(?code) datatype(?p)
                        WHERE {
                            ?x a dbo:City.
                            ?x dbo:areaCode ?code .
                            ?x ?p ?o .
                        } limit 1
    

    ...will return:

    However this will not differentiate between "resource" and "property" because a resource may be a property. What you probably mean is "individual" and "property" but even a property can be treated as an individual, for example in the triple rdfs:label rdfs:label "label".

    However you can always query the rdf:type of a resource, which may give you rdf:Property, owl:DatatypeProperty or owl:ObjectProperty.