简体   繁体   中英

Empty SPARQL Query Result

Trying to run a SPARQL query against a local ttl file. I have done this successfully before, but not this one. I suspect it has something to do with namespaces.

Tried printing out what the query was, and all I get is "rdflib.plugins.sparql.processor.SPARQLResult at 0x1fbe05d3400" so there IS somthing there.

Here is the ttl file

 # filename: ex050.ttl

 @prefix foaf: <http://xmlns.com/foaf/0.1/> . 

 <http://www.learningsparql.com/ns/demo#i93234>
         foaf:nick "Dick" ;  
         foaf:givenname "Richard" ; 
         foaf:mbox "richard49@hotmail.com" ;
         foaf:surname "Mutt" ;
         foaf:workplaceHomepage <http://www.philamuseum.org/> ;
         foaf:aimChatID "bridesbachelor" . 

And here is the python code firing off the SPARQL query

 filename = "C:/DataStuff/SemanticOntology/LearningSPARQLExamples/ex050.ttl" 
 import rdflib
 g = rdflib.Graph()

 result = g.parse(filename, format='ttl')
 query = """
 PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

 SELECT ?propertyLabel ?value 
 WHERE
 {
   ?s ?property ?value . 
   ?property rdfs:label ?propertyLabel . 
 }

 """

 results=g.query(query)
 print('Results!')
 for row in results:
     print(row)

I know it is something basic that I screwed up. Please help!

Okay - pouring over the RDFILB specs ( https://buildmedia.readthedocs.org/media/pdf/rdflib/4.2.1/rdflib.pdf ), I discovered that you can parse multiple files/urls one after the other. The result is a merged file. So knowing that the foaf file I was interested in could be downloaded at http://www.xmlns.com/foaf/spec/index.rdf

I got the file, then altered the program to:

filename = "C:/DataStuff/SemanticOntology/LearningSPARQLExamples/ex050.ttl" 
filename2 = "C:/DataStuff/SemanticOntology/LearningSPARQLExamples/index.rdf"

g = rdflib.Graph()

g.parse(filename, format='ttl')
g.parse(filename2)
query = """
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?propertyLabel ?value 
WHERE
{
  ?s ?property ?value . 
  ?property rdfs:label ?propertyLabel . 
}

"""

results=g.query(query)
print('Results!')
for row in results:
    print(row)

Which then worked, printing out:

Results!
(rdflib.term.Literal('Given name'), rdflib.term.Literal('Richard'))
(rdflib.term.Literal('Surname'), rdflib.term.Literal('Mutt'))
(rdflib.term.Literal('AIM chat ID'), rdflib.term.Literal('bridesbachelor'))
(rdflib.term.Literal('personal mailbox'), rdflib.term.Literal('richard49@hotmail.com'))
(rdflib.term.Literal('workplace homepage'), rdflib.term.URIRef('http://www.philamuseum.org/'))
(rdflib.term.Literal('nickname'), rdflib.term.Literal('Dick'))

printing out the labels of all the human readable properties in my original data file, even though I didn't define those properties.

I imagine that the step of downloading the file could be skipped by directly using the URL in the second parse statement. However I cannot do this as I am in a corporate environment and I don't see a way of using the parse statement with a proxy.

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