Re: Questions about lxml/ElementTree

Dave Kuhlman <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
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 
> 
[snip]
> 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.

Dave





_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.