lxml and namespaces

Marc Aymerich glicerinu at gmail.com
Tue Jul 29 14:35:55 EDT 2014


On Tue, Jul 29, 2014 at 8:19 PM, Marc Aymerich <glicerinu at gmail.com> wrote:

> Hi, I'm desperately trying to construct an XML with the following document
> declaration:
>
> <Document xmlns=”urn:iso:std:iso:20022:tech:xsd:pain.008.001.02”
>         xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
>
> I'm using LXML, and what I'm doing is this
>
> >>> from lxml import etree
> >>> from lxml.builder import E
> >>> doc = E.Document(
> ...     {
> ...         'xmlns': "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02",
> ...         '{xmlns}xsi': "http://www.w3.org/2001/XMLSchema-instance",
> ...     }
> ... )
> >>> print etree.tostring(doc)
> <Document xmlns:ns0="xmlns" ns0:xsi="
> http://www.w3.org/2001/XMLSchema-instance"
>         xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"/>
>
>
> I've tried everything I can imagine and this is the best I can do :(
>
> Any idea on how to get the xmlns and xmlns:xsi attributes correctly?
>


Got it!
xml = lxml.builder.ElementMaker(
     nsmap = {
         None: "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02",
         'xsi': "http://www.w3.org/2001/XMLSchema-instance",
     }
)
doc = xml.Document()


-- 
Marc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140729/fcb5a463/attachment.html>


More information about the Python-list mailing list