SPARQL - Unknown namespace prefix error

痞子三分冷 提交于 2019-12-11 14:24:09

问题


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?


回答1:


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/>



来源:https://stackoverflow.com/questions/50610084/sparql-unknown-namespace-prefix-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!