[XML-SIG] finding the NS URI for a prefix in a DOM
Alexandre
Alexandre.Fayolle@logilab.fr
Fri, 19 Jul 2002 17:42:43 +0200
On Fri, Jul 19, 2002 at 11:29:34AM -0400, Fred L. Drake, Jr. wrote:
>
> Alexandre writes:
> > I have a silly DOM question. I'm using 4DOM on this one, but this may
> > change later.
> >
> > What is the best way of finding the nsuri of a namespace given a prefix
> > to which the nsuri could be mapped?
>
> My own preference would be to use the lookupNamespaceURI() method on a
> node, but that would imply we have a Level 3 DOM. I suspect that
> minidom will eventually provide this in some form. ;-)
It's a long time since I've looked at the DOM TR drafts...
>
> 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.
What I've decided to use is XPath:
from xml.xpath import Evaluate
def prefix_to_nsuri(node):
map = {}
ns_nodes = Evaluate('namespace::node()',node)
for ns in ns_nodes:
map[ns.localName]=ns.value
return map
This works very well, and only needs a slight change to be able to get
the ns uri for one given prefix.
Alexandre Fayolle
--
LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
Narval, the first software agent available as free software (GPL).