首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在RDFLIB中使用前缀

在RDFLIB中使用前缀
EN

Stack Overflow用户
提问于 2011-01-11 00:17:24
回答 1查看 2.3K关注 0票数 5

我想在rdflib中使用短前缀来指定名称空间,但我遇到了麻烦。我想答案一定很简单。下面是有问题的代码:

代码语言:javascript
复制
g = rdflib.parse("some_rdf.rdf")

rdf=rdflib.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")

print "Name Spaces:"

for ns in g.namespaces():
    print ns

print "Matching Triples"
print "length of type full uri",len([i for i in g.triples((None,rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),None))])
print "length of type truncated uri",len([i for i in g.triples((None,rdflib.term.URIRef('rdf:type'),None))])
print "length of type , using namespace",len([i for i in g.triples((None,rdf.type,None))])

输出结果为:

代码语言:javascript
复制
Name Spaces:

('xml', rdflib.term.URIRef('http://www.w3.org/XML/1998/namespace'))
(u'foaf', rdflib.term.URIRef('http://xmlns.com/foaf/0.1/'))
(u'z', rdflib.term.URIRef('http://www.zotero.org/namespaces/export#'))
('rdfs', rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#'))
(u'bib', rdflib.term.URIRef('http://purl.org/net/biblio#'))
(u'dc', rdflib.term.URIRef('http://purl.org/dc/elements/1.1/'))
(u'prism', rdflib.term.URIRef('http://prismstandard.org/namespaces/1.2/basic/'))
('rdf', rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#'))
(u'dcterms', rdflib.term.URIRef('http://purl.org/dc/terms/'))
Matching Triples
length of type full uri 132
length of type truncated uri 0 !!!This is wrong should be 132
length of type , using namespace 132

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-11 01:16:16

RDFLib不支持您在第二种情况下尝试使用它的方式。你可以这样做...

rdf=rdflib.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")

rdflib.term.URIRef(rdf+'type')

rdflib.term.URIRef(rdf['type'])

我很喜欢他们在你的第三个案例中表达出来的方式,为什么不坚持这个呢?

顺便说一句-- RDF名称空间已经在RDFLib中创建,您可以这样做...

代码语言:javascript
复制
from rdflib.namespace import RDF
#RDF <-- rdf.namespace.ClosedNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4648873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档