[lxml-dev] namespace strangeness in lxml 1.1

Stefan Behnel stefan_ml at behnel.de
Wed Aug 20 09:07:55 CEST 2008


Hi,

coming back to this after a while...

Eric Jahn wrote:
> On Tue, 2008-07-01 at 07:20 +0200, Stefan Behnel wrote:
>> If you want it to replace the namespace by a resolved prefix, use
>>
>>     type = etree.QName(NS2 + "...")
> 
> No, I don't want the prefix resolved the the url, so I guess my only
> option is to do something like the following and just pass the type
> value a string with the namespace prefix explicity stated: 
> 
> child1 = etree.SubElement(root,NS2 + "secondelement", nsmap=NSMAP, type
> = "NS2:someattribute")

I think you misunderstood my example (and apparently didn't try it on your
side). Isn't this what you wanted:

    >>> import lxml.etree as et
    >>> root = et.XML('<root xmlns:a="http://my/ns"><el/></root>')
    >>> root[0].set("type", et.QName("{http://my/ns}tname"))
    >>> et.tostring(root)
    '<root xmlns:a="http://my/ns"><el type="a:tname"/></root>'

This has been working for quite a while now.

Stefan




More information about the lxml-dev mailing list