【问题标题】:SPARQL - Unknown namespace prefix errorSPARQL - 未知的命名空间前缀错误
【发布时间】:2018-11-09 15:03:56
【问题描述】:

我有一个导入了 rdflib 并实现了一些 SPARQL 查询的 python 文件

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 打印出来没有问题,但是对于 res2,我收到一条错误消息:

Exception: Unknown namespace prefix : weather

根据我用来实现此功能的编辑器 pycharm,这显然是由于第 15 行出现错误。

我错过了什么导致此错误? 在我的 SPARQL 查询中调用 weather:region 是否还有更多内容? 如果是这样如何解决这个问题?

【问题讨论】:

    标签: python pycharm sparql rdflib rdfa


    【解决方案1】:

    正如错误消息所暗示的,命名空间 weather: 未定义 - 因此在 SPARQL 中,您需要使用前缀来定义天气,例如:

    PREFIX weather: <weatheruri>

    或者您应该使用明确的天气 URI 而不是 weather:

    天气名称空间 URI(或者称为 IRI?)将位于 rdf 文档的 XML 名称空间中 - 它将以 / 或 # 结尾,因此如果 URI 是 http://weather.com/,则前缀定义是 PREFIX weather: &lt;http://weather.com/&gt;

    【讨论】:

    • 好的,我在我的 rdf 文档中添加了前缀,这解决了问题。谢谢。
    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多