[lxml-dev] Handling namespaces in tags

Stefan Behnel stefan_ml at behnel.de
Fri Oct 24 10:44:07 CEST 2008


Hi,

John Krukoff wrote:
> 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

I actually like the way this looks. However, I'd make it read-only, i.e. only

    local_name = someElement.tag.localname
    namespace = someElement.tag.namespace

will work. For the update case, there's

    someElement.tag = etree.QName(namespace, tag)

Making this read-only also avoids any problems with smart strings keeping
Elements alive, as they wouldn't need a reference to an Element. Knowing their
underlying tag string is sufficient.

If someone wants to take a shot on this, please look at the way smart strings
are implemented for XPath in extensions.pxi. The _getNsTag() function in
apihelpers.pxi already does what's needed here.

Regarding symmetry, BTW, wouldn't

    local_name = etree.QName(someElement.tag).localname
    namespace = etree.QName(someElement.tag).namespace

work better?

Stefan



More information about the lxml-dev mailing list