rdflibjsonld在“@id”中使用“<”时生成虚假url

2024-05-12 20:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用rdflibjsonld解析一些NoSQL数据并插入到Sesame中。这是代码中有问题的部分:

context = {
    "@context": {
    "isGiven": URIRef('<'+'http://purl.org/net/something#isGiven'+rdfLizerItem['FooByBar']+'>'),
    "administeredAs": URIRef('<'+'http://purl.org/net/something#administeredAs'+'>'),
    "type":URIRef('<'+'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'+'>'),
    }
    }
    recipient=URIRef('<'+someUrl+rdfLizerItem['FooRecipient']+'>')
    jsonldOutputIsAdmin = {"@id": recipient,"isGiven": URIRef('<'+someUrl+ rdfLizerItem['Quantity']+'>')}
    print jsonldOutputIsAdmin
    g = Graph()
    g.parse(data=json.dumps(jsonldOutputIsAdmin), format='json-ld', context=context)
    g.close()
    for s,p,o in g:
        pprint.pprint ((s,p,o))

问题是在<>添加到@id的URL之后,主题的URL就变成了它的完整路径。例如:

(rdflib.term.URIRef(u'file:///C:/path/to/the/url/<http:/purl.org/net/ontologyName#subject>'),
 rdflib.term.URIRef(u'<http://purl.org/net/ontologyName#predicate>'), 
rdflib.term.Literal(u'<http://purl.org/net/ontologyName#object>'))

我只想在主题的网址,而不是文件路径。是什么导致了这个问题?我该如何解决它?你知道吗

我需要<>将三元组导出到Sesame。你知道吗


Tags: orghttpnetcontextsomethingrdflibtermpurl
1条回答
网友
1楼 · 发布于 2024-05-12 20:25:28

为了避免将文件系统路径作为前缀应用,需要为上下文提供base IRI的值

JSON-LD allows IRIs to be specified in a relative form which is resolved against the document base according section 5.1 Establishing a Base URI of [RFC3986]. The base IRI may be explicitly set with a context using the @base keyword.

例如,如果您这样修改上下文:

context['@context']['@base'] = '.'

您将看不到以@id值为前缀的路径。你知道吗

相关问题 更多 >