-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NamespaceManager.bind(prefix, namespace, replace=True) leaves existing prefix in place #543
Comments
The confusion here is the two arguments Say they release FOAF 2.0 (Now based on google+? :) ) and you want to change Now you want to "upgrade" to FOAF 2.0 :
Adding:
In the FOAF2 example, you probably want both. |
Thanks very much for taking the trouble to respond. I appreciate it. However, while your solution works in the sense that it does remove the old binding to foaf, it is not a complete answer to my question. What I want to achieve in my code is to remove the old prefix altogether. In that I have not succeeded, with any combinations of replace and override. I also tried to rebind to Namespace(None) and Namespace(''), but to no avail. What I am looking for is an "unbind" method I suppose. Or is there some solution I have not thought of? Thanks again. Small code example follows. The attempt to remove foaf2 is at the bottom, where I bind foaf3, which works but leaves foaf2 also in place: from rdflib import Graph, Namespace
g = Graph()
g.namespace_manager.bind('foaf', Namespace('http://xmlns.com/foaf/0.1'))
g.namespace_manager.bind('foaf2', Namespace('http://xmlns.com/foaf/2.0'))
print('--- namespaces after initial bind ---')
for ns in g.namespaces():
print('%s:%s' % (ns))
print
#--- namespaces after initial bind ---
#xml:http://www.w3.org/XML/1998/namespace
#foaf:http://xmlns.com/foaf/0.1
#rdfs:http://www.w3.org/2000/01/rdf-schema#
#rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
#xsd:http://www.w3.org/2001/XMLSchema#
#foaf2:http://xmlns.com/foaf/2.0
g.namespace_manager.bind('foaf', Namespace('http://xmlns.com/foaf/2.0'), replace=True)
print('--- namespaces after rebind ===')
for ns in g.namespaces():
print('%s:%s' % (ns))
print
#--- namespaces after rebind ===
#xml:http://www.w3.org/XML/1998/namespace
#foaf:http://xmlns.com/foaf/2.0
#rdfs:http://www.w3.org/2000/01/rdf-schema#
#rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
#xsd:http://www.w3.org/2001/XMLSchema#
#foaf2:http://xmlns.com/foaf/2.0
g.namespace_manager.bind('foaf3', Namespace('http://xmlns.com/foaf/2.0'), override=True, replace=True)
print('--- namespaces after second rebind ===')
for ns in g.namespaces():
print('%s:%s' % (ns))
#--- namespaces after second rebind ===
#xml:http://www.w3.org/XML/1998/namespace
#foaf:http://xmlns.com/foaf/2.0
#rdfs:http://www.w3.org/2000/01/rdf-schema#
#rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
#xsd:http://www.w3.org/2001/XMLSchema#
#foaf3:http://xmlns.com/foaf/2.0
#foaf2:http://xmlns.com/foaf/2.0 |
Hmm you are right - this IS a bug. The problem is (maybe) in the individual implementation, for instance, here: https://github.com/RDFLib/rdflib/blob/master/rdflib/plugins/memory.py#L150 that when replacing, the Also, an |
In PR #1094 , We added an unbind function which can be called in same way in which we call bind function. We pass the prefix that need to be unbind as parameter. The example can be given as:- from rdflib import Graph, Namespace |
Recently fixed in #1843 def test_namespace_bind():
from rdflib import Graph, Namespace
g = Graph()
g.namespace_manager.bind("foaf", Namespace("http://xmlns.com/foaf/0.1"))
g.namespace_manager.bind("foaf2", Namespace("http://xmlns.com/foaf/2.0"))
logger.debug("--- namespaces after initial bind ---")
for ns in g.namespaces():
logger.debug("%s:%s" % (ns))
# ORIG
# --- namespaces after initial bind ---
# xml:http://www.w3.org/XML/1998/namespace
# foaf:http://xmlns.com/foaf/0.1
# rdfs:http://www.w3.org/2000/01/rdf-schema#
# rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
# xsd:http://www.w3.org/2001/XMLSchema#
# foaf2:http://xmlns.com/foaf/2.0
# FIXED
# --- namespaces after initial bind ---
# owl:http://www.w3.org/2002/07/owl#
# xml:http://www.w3.org/XML/1998/namespace
# foaf:http://xmlns.com/foaf/0.1
# rdfs:http://www.w3.org/2000/01/rdf-schema#
# rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
# xsd:http://www.w3.org/2001/XMLSchema#
# foaf2:http://xmlns.com/foaf/2.0
g.namespace_manager.bind(
"foaf", Namespace("http://xmlns.com/foaf/2.0"), replace=True
)
logger.debug("--- namespaces after rebind ===")
for ns in g.namespaces():
logger.debug("%s:%s" % (ns))
# ORIG
# --- namespaces after rebind ===
# xml:http://www.w3.org/XML/1998/namespace
# foaf:http://xmlns.com/foaf/2.0
# rdfs:http://www.w3.org/2000/01/rdf-schema#
# rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
# xsd:http://www.w3.org/2001/XMLSchema#
# foaf2:http://xmlns.com/foaf/2.0
# FIXED
# --- namespaces after rebind ===
# owl:http://www.w3.org/2002/07/owl#
# xml:http://www.w3.org/XML/1998/namespace
# foaf:http://xmlns.com/foaf/2.0
# rdfs:http://www.w3.org/2000/01/rdf-schema#
# rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
# xsd:http://www.w3.org/2001/XMLSchema#
g.namespace_manager.bind(
"foaf3", Namespace("http://xmlns.com/foaf/2.0"), override=True, replace=True
)
logger.debug("--- namespaces after second rebind ===")
for ns in g.namespaces():
logger.debug("%s:%s" % (ns))
# ORIG
# --- namespaces after second rebind ===
# xml:http://www.w3.org/XML/1998/namespace
# foaf:http://xmlns.com/foaf/2.0
# rdfs:http://www.w3.org/2000/01/rdf-schema#
# rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
# xsd:http://www.w3.org/2001/XMLSchema#
# foaf3:http://xmlns.com/foaf/2.0
# foaf2:http://xmlns.com/foaf/2.0
# FIXED
# --- namespaces after second rebind ===
# owl:http://www.w3.org/2002/07/owl#
# xml:http://www.w3.org/XML/1998/namespace
# rdfs:http://www.w3.org/2000/01/rdf-schema#
# rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#
# xsd:http://www.w3.org/2001/XMLSchema#
# foaf3:http://xmlns.com/foaf/2.0 |
I want to change an existing and bound namespace prefix. From the documentation of NamespaceManager I understand that "bind" with parameter "replace=True" should achieve this. However, while this does associate the namespace with the new prefix, the old prefix remains bound to the same namespace.
This can make it impossible to obtain a serialization with the new prefix. Is this intended behaviour?
Code example:
The text was updated successfully, but these errors were encountered: