[lxml-dev] Handling namespaces in tags

John Krukoff jkrukoff at ltgc.com
Fri Oct 24 00:02:07 CEST 2008


On Thu, 2008-10-23 at 21:49 +0200, Stefan Behnel wrote:
> 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
> _______________________________________________
> lxml-dev mailing list
> lxml-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/lxml-dev

I wrote a function like this that splits into a tuple, but it turned out
that the only thing I used it for was when moving an element from one
namespace to another, thus always ignoring the namespace part of the
tuple.

I can understand that there's some risks for future ElementTree
compatibility, but a new attribute like Element.localtag or
Element.localname (Possible name idea to pull terminology from XSLT
local-name?) and Element.namespace would make it easy to use the parts
independently.

Even in my own code the above use case isn't common, but having
assignable attributes of that type would provide the simplest solution,
turning

name, namespace = split_qname( someElement.tag )
someElement.tag = "{%s}%s" % ( newNamespace, name )

into

someElement.namespace = newNamespace

If there wasn't the recent object lesson with smart strings for xpath
results to show the backwards incompatibility issues, I'd probably
advocate for a smart string type object for tag names that provided
attributes to access the name and namespace parts, something like:

someElement.tag.namespace = newNamespace

Personally though? I wouldn't waste the time implementing it, it can't
possibly be that common a thing to need.

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



More information about the lxml-dev mailing list