
Hi, My Schema contains something like this: <complexType name="TypeWithNullElements"> <sequence> <element name="nillableElem" nillable="true" type="int"/> <element name="minOccursElem" minOccurs="0" type="int"/> </sequence> </complexType> and I would like to build an XML elements in python/lxml that will render to: <typeWithNullElements> <nillableElem xsi:nil="true"/> </typeWithNullElements> But I don't konow how to set the nillableElem to be equal Nil. Thank you for your time.

Hi,
and I would like to build an XML elements in python/lxml that will render to:
<typeWithNullElements> <nillableElem xsi:nil="true"/> </typeWithNullElements>
But I don't konow how to set the nillableElem to be equal Nil. Thank you for your time.
Especially easy if you use lxml.objectify:
root = objectify.Element('root') root.sub_element = None print etree.tostring(root, pretty_print=True) <root xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE"> <sub_element xsi:nil="true"/> </root>
With lxml.etree:
root = etree.Element('root') sub_element = etree.Element('sub_element') sub_element.set('{http://www.w3.org/2001/XMLSchema-instance}nil', 'true') root.append(sub_element) print etree.tostring(root, pretty_print=True) <root> <sub_element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </root>
You might also want to take a look at the E-Factory (http://lxml.de/tutorial.html#the-e-factory) or its objectify notion (http://lxml.de/objectify.html#tree-generation-with-the-e-factory) Holger -- NEU: FreePhone - kostenlos mobil telefonieren! Jetzt informieren: http://www.gmx.net/de/go/freephone
participants (2)
-
Damian Powązka
-
jholg@gmx.de