0

I am trying to write SPARQL queries using rdflib in python and here below is my code. I want the queries just to return the values of the class or the entities without the URI or the namespace. I don't want to parse the results using regex. I am looking for any built in function in rdflib.

from rdflib import Graph

g = Graph()
g.parse("/Users/SUV/Desktop/AlfredOWL2.owl")

qres = g.query("""PREFIX ex:<http://www.semanticweb.org/suv/ontologies/2015/3/alfredowl/> SELECT ?Mood ?Action WHERE { 
          ?Mood rdf:type ex:Mood .
          ?Mood ex:possible ?Action}""")

for row in qres:
 print row.Mood, row.Action
suv_12
  • 3
  • 2
  • 1
    What do you mean by "values of the class or the entities without the URI or the namespace". The value of ?Mood has to be either a URI resource or a blank node. Those are the only things that can be subjects in RDF. The value of ?Action can be a URI resource, a blank node, or a literal. What do you mean by "values"? – Joshua Taylor Apr 25 '15 at 23:45
  • But if you're trying to get just the suffix of some IRI (i.e., the part after some prefix that you know), you can do that in the SPARQL query itself. E.g., see the answer to [why my SPARQL query return URI of a class instead of its name?](http://stackoverflow.com/a/28481688/1281433) or [retrieving the class name of a specific subclass in owl](http://stackoverflow.com/q/20830056/1281433) (which actually references RDFlib). Is this what you're looking for? – Joshua Taylor Apr 25 '15 at 23:49
  • Maybe the AlfredOWL2.owl also has hasName data properties, so the query may be changed to fetch those by adding ?Mood ex:hasName ?MoodName – Roman Susi May 03 '15 at 18:18
  • @Joshua Taylor,That was what I was looking for. Thanks for the solution. – suv_12 Jul 04 '15 at 23:19

0 Answers0