[XML-SIG] finding the NS URI for a prefix in a DOM
Rich Salz
rsalz@datapower.com
Fri, 19 Jul 2002 11:51:35 -0400
Fred L. Drake, Jr. wrote:
> I don't think we have a good way to do that now, but it could be done
> in a DOM-independent way by walking up the tree looking at namespace
> declarations.
That's what I do in ZSI. Here's the code, which might be of use ...
_attrs = lambda E: (E.attributes and E.attributes.values()) or []
def GetElementNSdict(self, elt):
'''Get a dictionary of all the namespace attributes for an
element. The dictionaries are cached, and we recurse up the tree
as necessary.
'''
d = self.ns_cache.get(id(elt))
if not d:
if elt != self.dom: d = self.GetElementNSdict(elt.parentNode)
for a in _attrs(elt):
if a.namespaceURI == XMLNS.BASE:
if a.localName == "xmlns":
d[''] = a.nodeValue
else:
d[a.localName] = a.nodeValue
self.ns_cache[id(elt)] = d
return d.copy()