pythonpycharmsparqlrdflibrdfa

SPARQL - Unknown namespace prefix error


I have a python file with imported rdflib and some SPARQL query implemented

from rdflib import Graph
import html5lib

if __name__ == '__main__':
    g = Graph()

    g.parse('http://localhost:8085/weather-2.html', format='rdfa')

res1 = g.parse('http://localhost:8085/weather-2.html', format='rdfa')
print(res1.serialize(format='pretty-xml').decode("utf-8"))
print()

res2 = g.query("""SELECT ?obj
    WHERE { <http://localhost:8085/weather-2.html> weather:region ?obj . }
    """)
for row in res2:
    print(row)

res1 has no trouble to print out but for res2 I get an error saying:

Exception: Unknown namespace prefix : weather

Apparently this is due to an error on line 15 according to pycharm, the editor I am using to implement this.

What am I missing that is causing this error? Is there more to just calling weather:region in my SPARQL query? If so how to do fix this problem?


Solution

  • As the error message suggests, the namespace weather: is not defined - so in the SPARQL you either need a PREFIX to define weather, like:

    PREFIX weather: <weatheruri>

    Or you should put the explicit weather URI instead of the weather:

    The weather namespace URI (or is it called an IRI?) will be in the XML namespaces in the rdf document - it will end with / or # so if the URI is http://weather.com/ the prefix definition is PREFIX weather: <http://weather.com/>