from rdflib import Graph, Namespace
from rdflib.namespace import RDF
g = Graph()
NS = Namespace("http://example.com/")
# then, say Xxx is a class and Aaa is an instance of Xxx...
g.add((NS.Aaa, RDF.type, NS.Xxx))
# so use NS.Xxx (or NS["Xxx"]) to get a URIRef of NS.Xxx from Namespace NS
print(type(NS)) # --> <class 'rdflib.term.URIRef'>
print(type(NS.Xxx)) # --> <class 'rdflib.term.URIRef'>
print(NS.Xxx) # --> "http://example.com/Xxx"
If you want to bind a prefix within a graph, you use the rdflib Graph
class' bind()
method so, for the code above, you would use:
g.bind("ns", NS)
Now the graph, if serialized with a format that knows about prefixes, like Turtle, will use "ns". The above data would be:
@prefix ns: <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ns:Aaa rdf:type ns:Xxx .
So, in Python, if you want to make the URI "http://example.com/Xxx"" from the string "ns:Xxx" you should have everything you need:
- the namespace declaration:
NS = Namespace("http://example.com/")
- the prefix/namespace binding
g.bind("ns", NS)
IFF, on the other hand, you didn't declare the namespace yourself but it's in a graph and you only have the short form URI "ns:Xxx", you can do this to list all bound prefixes & namespaces used in the graph:
for n in g.namespace_manager.namespaces():
print(n)
returns, for the data above:
('xml', rdflib.term.URIRef('http://www.w3.org/XML/1998/namespace'))
('rdf', rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#'))
('rdfs', rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#'))
('xsd', rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#'))
('eg', rdflib.term.URIRef('http://example.com/'))
So, if you know "eg:Xxx", you can split off the "eg" part and make the URI you want like this:
print(
[str(x[1]) for x in g.namespace_manager.namespaces() if x[0] == s.split(":")[0]]
[0] + s.split(":")[1]
)
prints:
http://example.com/Xxx