[lxml-dev] Handling namespaces in tags

Stefan Behnel stefan_ml at behnel.de
Thu Oct 23 21:49:27 CEST 2008


Hi,

David Soulayrol wrote:
> Is there some utility with lxml to retrieve the namespace and the name
> of a tag, or do we have to write on our own something like the
> following, anytime we need it ?
> 
> ns, tag = node.tag[1:].find('}')

I assume you meant .split('}') here.

There isn't a dedicated utility function for it. I actually run into this
problem less frequently than one might think, as I rarely really need the tag
name where I can't use it together with the namespace. My guess is that this
happens most frequently where different XML languages are handled by the same
code.

I admit that this might be a nice thing to add, though, as people who need
this really have to write more or less the same code each time. We could call
it "splittag()" - or maybe someone has a better idea? I could also imagine to
let it accept Element objects and return their ns-tag as a tuple. That's more
efficient than first building and then splitting the tag name again.

Stefan


More information about the lxml-dev mailing list