简体   繁体   中英

How to get data type property values using SPARQL

I have created some sample ontology in protege.According to my ontology there is a class called person and which has sub class called Student.There are some student individuals(john,paul,marry,...). I have defined some data property called "email" and assigned their email addresses.

Following query which is resulting all the individuals in ontology.But I want to get each individual and their email address.

String queryStr =
    "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  "+
    "select   ?ind "+
    "where { "+
    "?ind  rdf:type <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#Student> ;"+

"}\n ";

Above query was tested on jena in eclipse IDE.

any idea..?

Thank in advance!

To get the email addresses you need to 1) add a variable for them to the SELECT line, and 2) bind that variable in the WHERE pattern. And you'll probably want to add a prefix for your own ontology, since you'll now need to refer to it twice. Something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#>

SELECT ?ind ?email
WHERE {
  ?ind rdf:type my:Student .
  ?ind my:Email ?email
}

A more compact way would be the following:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#>

SELECT ?ind ?email 
WHERE { 
       ?ind rdf:type my:Student ;
            my:Email ?email .
     }

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