简体   繁体   中英

how to extract subject (Name) in comparison with predicate from a RDF Triple using sparql

http://www.example.com/teach.rdfs/John   http://www.example.com/teach.rdfs#position "Full Professor"        

http://www.example.com/teach.rdfs/John   http://www.example.com/teach.rdfs#course"Math"

http://www.example.com/teach.rdfs/John   http://www.example.com/teach.rdfs#student"Undergraduate"

http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#position "Assistant Professor"

http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#course"Web Engineering"

http://www.example.com/teach.rdfs/Arthur http://www.example.com/teach.rdfs#student"Graduate"

IF these are the triples and i want to Find assistant professors who teach Graduate

 Lecturer       Position

 Arthur         Assistant Professor 

how it is possible to extract the above date using SPARQL

Your data isn't in any legal RDF serialization that I know of, but it is fairly easy to get it into the N3 serialization. It is rather unusual to see both http://.../teach.rdfs# and http://.../teach.rdfs/ used as prefixes in the same document. It's common to see one or the other, but not both. It's not illegal though, so we can work with it. In N3 format, here is your data as a file, data.n3 :

@prefix teach1: <http://www.example.com/teach.rdfs/> .
@prefix teach2: <http://www.example.com/teach.rdfs#> .

teach1:John teach2:position "Full Professor" .
teach1:John teach2:course "Math" .
teach1:John teach2:student "Undergraduate" .
teach1:Arthur teach2:position "Assistant Professor" .
teach1:Arthur teach2:course "Web Engineering" .
teach1:Arthur teach2:student "Graduate" .

The query is pretty simple too. Here is as, as a file called query.sparql :

PREFIX teach1: <http://www.example.com/teach.rdfs/>
PREFIX teach2: <http://www.example.com/teach.rdfs#>

SELECT ?lecturer ?position WHERE {
  VALUES ?position { "Assistant Professor" }
  ?lecturer teach2:position ?position ;
            teach2:student "Graduate" .
}

The only thing that is a bit unusual about this query is the use of VALUES ?position { "Assistant Professor" } . The reason that I used the VALUES form is that your desired results included the "Assistant Professor" in the output. If we exclude the VALUES ... part, we can rewrite the pattern as

  ?lecturer teach2:position "Assistant Professor" ;
            teach2:student "Graduate" .

and still find the same ?lecturer s, but there is no variable bound to "Assistant Professor" . With the data and query in hand, we can run the query against the data using Jena's ARQ command line tools:

$ arq --query query.sparql --data data.n3 
-----------------------------------------
| lecturer      | position              |
=========================================
| teach1:Arthur | "Assistant Professor" |
-----------------------------------------

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