[lxml-dev] Handling namespaces in tags

John Krukoff jkrukoff at ltgc.com
Fri Oct 24 20:29:07 CEST 2008


On Fri, 2008-10-24 at 10:50 +0200, Stefan Behnel wrote:
> Stefan Behnel wrote:
> >     local_name = etree.QName(someElement.tag).localname
> >     namespace = etree.QName(someElement.tag).namespace
> 
> We could even allow passing an Element instead of a tag, such as
> 
>       local_name = etree.QName(someElement).localname
>       namespace = etree.QName(someElement).namespace
> 
> I think that's even better than the other solutions so far.
> 
> Stefan
> _______________________________________________
> lxml-dev mailing list
> lxml-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/lxml-dev

Personally, I like this approach best because (as you've no doubt
noticed from my bug reports), I already pass around names a QName
objects all the time, so it's useful to me to get this functionality
there, rather than having to go through a somewhat laborious element
instantiation to get from a QName to an actual element.

My only note is that I expect most people who use lxml or ElementTree
don't even know that the QName class exists, as it's not an important
(or particularly useful) part of the API. The only reason I started
using it, is that it's a convenient way to do the reverse of what we've
been talking about; stick a namespace and a local name back together.

Which, now that I mention it, sounds like an excellent reason to use it
for the inverse as well.

Should you decide to give QNames these attributes, and make them
non-opaque objects, it would also be useful to add a .tag attribute to
pull the entire formatted name from them. Or at least document the .text
attribute as doing the same so that it can be depended on.

-- 
John Krukoff <jkrukoff at ltgc.com>
Land Title Guarantee Company



More information about the lxml-dev mailing list