在RDFLIB中使用前缀
我想在rdflib中使用一个简短的前缀来指定一个命名空间,但我遇到了一些问题。我觉得答案应该很简单。这里是出问题的代码:
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))])
输出结果是:
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
我哪里做错了呢?
1 个回答
5
你在第二种情况下使用的方式是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中已经创建好了,你可以这样做...
from rdflib.namespace import RDF
#RDF <-- rdf.namespace.ClosedNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')