[XML-SIG] Questions about lxml/ElementTree

Dave Kuhlman dkuhlman at rexx.com
Thu Jun 14 00:16:00 CEST 2007

Fredrik Lundh <fredrik <at> pythonware.com> writes:

> Dave Kuhlman wrote:
> > 1. When I get the tag for a node (element) using node.tag, I see
> >    something like this::
> > 
> >        {http://xxxx.com/ns/yyyy}zzzz
> > 
> >    The stuff inside curly brackets is the namespace.  I don't need
> >    that, so I use a regular expression to strip it off.
> > 
> >    My question is -- Is there a way to get the tag (element name)
> >    without a namespace.  I'll feel silly at some time in the
> >    future after writing lots of code that strips the namespace
> >    if I find that there is an easier way.
> you'll probably feel even sillier when someone adds an element with the 
> same tag but in a different namespace to the data you're dealing with, 
> and your program breaks in a really strange way 
> first, the namespace *is* part of the element name.  you should only 
> ignore it if you know exactly what you're doing.  ("don't know what it's 
> good for" isn't a valid reason 
> hope this helps!

Yes.  That helps a lot.  Thanks to you, I'm not ignoring namespaces, now.

So, one more question -- If I want to check for a specific tag (for example, 
document) in the default namespace, I would use the following:

    if el.tag == '{%s}document' % el.nsmap[None]:

Is that right?

Is there some document that I can read that answers questions like this and 
that will tell me how to do namespace related tasks in a pythonic way?

Thanks again for help.


More information about the XML-SIG mailing list