我有一个现有的 rdf 图,我想修改它。我想在 n 变量中添加信息。
# source graph
g = source.graph
#new entry
n = (URIRef(obj.pid), URIRef('info:symplectic/symplectic-elements:def/model#hasPublicUrl'), URIRef('http://itemlocation.com'))
#add to graph
g.add(n)
#save back to source
source.graph = g
输出:
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:fedora-model="info:fedora/fedora-system:def/model#"
xmlns:ns1="info:symplectic/symplectic-elements:def/model#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="info:fedora/sympOE:mq5zf">
<fedora-model:hasModel rdf:resource="info:fedora/emory-control:PublishedArticle-1.0"/>
</rdf:Description>
<rdf:Description rdf:about="sympOE:mq5zf">
<ns1:hasPublicUrl rdf:resource="http://itemlocation.com"/>
</rdf:Description>
</rdf:RDF>
一切几乎都是正确的,但我希望它在两个方面有所不同:
- 我希望只有一个 rdf:Description 部分包含这两个条目。
- 我希望 ns1 别名为 symp
感谢您的帮助!
最佳答案
I want there to only be one rdf:Description section that contains both entries.
您这里似乎有两个 URI info:fedora/sympOE:mq5zf
和 sympOE:mq5zf
。
尝试将新的输入行重写为:
uri = URIRef('info:fedora/' + obj.pid)
g.add(
(uri, URIRef('info:symplectic/symplectic-elements:def/model#hasPublicUrl'), URIRef('http://itemlocation.com'))
)
I want the ns1 alias to be symp
您需要将命名空间绑定(bind)到图表。请参阅 this page 底部的示例.
它看起来像:
from rdflib import Namespace
ns = Namespace('http://localhost/symp#')
g.bind('symp', ns)
关于Python rdflib 嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860393/