5

I would like to use a short prefix to designate a namespace in rdflib but I am having trouble. I think the answer must be very simple. Here is the offending code:

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))])

And the output is:

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

What am I doing wrong?

tjb
  • 11,480
  • 9
  • 70
  • 91

1 Answers1

5

They way you are trying to use it in your second case is not supported by RDFLib. You could do like ...

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

and

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

or

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

I quite like they way it's expressed in your third case, why not sticking to that one ?

BTW - the RDF namespace is already created in RDFLib you can do ...

from rdflib.namespace import RDF
#RDF <-- rdf.namespace.ClosedNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
  • Thank you, this works. I agree that the third way is nice and I will use it from now on. – tjb Jan 11 '11 at 08:54
  • You can actually just use `RDF.type` (or `RDF['type']`, depending on the style you prefer) as an abbreviation of the third case, after having imported RDF as described above (`from rdflib.namespace import RDF`). – Sander Vanden Hautte Sep 12 '19 at 11:56