Search code examples
typespropertiessparql

SPARQL - Get object properties and exact type of datatype properties of a class


I want to get all the properties of a class. If a property is a datatype property I want to know its exact type i.e. float, integer, date etc.

Running the following query I get only if a property is an object property or a datatype property

SELECT ?class ?property ?type
WHERE {
    ?resource ?property ?target .
    ?property rdfs:domain ?class .
    ?resource a ?class .
    ?property rdf:type ?type .

}

Results

:Store  :location http://www.w3.org/2002/07/owl#ObjectProperty
:Product :price http://www.w3.org/2002/07/owl#DatatypeProperty

How can I specify the type of a property if it is a datatype property? For example, I would like to know not only that price is a DatatypeProperty but it is a float.

Thank you.


Solution

  • The datatype gets specified as the range (rdfs:range) of a datatype property.

    SELECT ?class ?property ?type ?range
    WHERE {
    
        ?resource ?property ?target .
        ?resource a ?class .
        ?property rdf:type ?type .
    
        OPTIONAL {    
          ?property rdfs:domain ?class .
        }
    
        OPTIONAL {
          ?property rdfs:range ?range .
        }
    
    }
    

    (I used OPTIONAL so that your query also lists properties for which no domain and/or range is specified.)

    Result: Screenshot