<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><style>p {margin:0px;padding:0px;} blockquote { border: 0px; margin-top: 0px; margin-bottom: 0px; }</style></head><body style="">
Hi David,<br><p><font color="#000000" face="Verdana" size="2"><br><blockquote type="cite">Does anyone have a simple example with a short XML file containing some<br>elements, its schema, and the python code parsing the file using<br>objectify with the xsi namespace to do type association ?<br><br></blockquote><br></font></p><p><br></p><p>I'm not quite sure I understand what you try to achieve,</p><p>but lxml.objectify does not necessarily need a schema:</p><p>&nbsp;</p><p>&nbsp;$ cat simpleInstance.xml<br>&lt;root&gt;<br>&nbsp;&nbsp;&nbsp; &lt;s&gt;A string, hopefully.&lt;/s&gt;<br>&lt;/root&gt;</p><p> &nbsp;</p><p>You can simply parse this using objecify:</p><p>&nbsp;</p>&gt;&gt;&gt; from lxml import etree, objectify<br>&gt;&gt;&gt; root = objectify.parse("simpleInstance.xml").getroot()<br>&gt;&gt;&gt; print objectify.dump(root)<br>root = None [ObjectifiedElement]<br>&nbsp;&nbsp;&nbsp; s = 'A string, hopefully.' [StringElement]<br>&gt;&gt;&gt; print root.s<br>A string, hopefully.<br>&gt;&gt;&gt;<br><p>&nbsp;</p><p>What you can do is validate this XML tree against a schema:</p><p>&nbsp;</p><p>$ cat simpleSchema.xsd<br>&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;<br>&nbsp;<br>&nbsp; &lt;xsd:element name="root" type="RootType"/&gt;<br>&nbsp;<br>&nbsp; &lt;xsd:complexType name="RootType"&gt;<br>&nbsp;&nbsp;&nbsp; &lt;xsd:sequence&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="s" type="xsd:string"/&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/xsd:sequence&gt;<br>&nbsp; &lt;/xsd:complexType&gt;<br>&lt;/xsd:schema&gt; <br></p><p>&nbsp;</p><p>&gt;&gt;&gt; from lxml import etree, objectify<br>&gt;&gt;&gt; root = objectify.parse("simpleInstance.xml").getroot()<br>&gt;&gt;&gt; schema = etree.XMLSchema(objectify.parse("simpleSchema.xsd"))<br>&gt;&gt;&gt; schema.validate(root)<br>True<br>&gt;&gt;&gt; </p><p>&nbsp;</p><p>No xsi:type information anywhere, so far.</p><p>&nbsp;</p><p>What you currently can not do is use the schema to *add* xsi:type attributes</p><p>to the XML instance. Or, to put it another way, schema-validation does not</p><p>add any type-information.&nbsp;</p><p>&nbsp;</p><p>No problem, though, if the XML contains xsi:type information:</p><p>&nbsp;</p><p>&gt;&gt;&gt; from lxml import etree, objectify<br>&gt;&gt;&gt; root = objectify.parse("simpleInstance2.xml").getroot()<br>&gt;&gt;&gt; print objectify.dump(root)<br>root = None [ObjectifiedElement]<br>&nbsp;&nbsp;&nbsp; s = 'A string, hopefully.' [StringElement]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * xsi:type = 'xsd:normalizedString'<br>&gt;&gt;&gt; print root.s<br>A string, hopefully.<br>&gt;&gt;&gt; schema = etree.XMLSchema(objectify.parse("simpleSchema.xsd"))<br>&gt;&gt;&gt; schema.validate(root)<br>True<br>&gt;&gt;&gt;<br></p><p>&nbsp;</p><p>If xsi:type information is available, it will be used to determine the lxml.objectify</p><p>type representation of an element:</p><p>Consider</p><p>&nbsp;</p><p>&gt;&gt;&gt; root = objectify.fromstring("&lt;root&gt;&lt;s&gt;3&lt;/s&gt;&lt;/root&gt;")<br>&gt;&gt;&gt; print objectify.dump(root)<br>root = None [ObjectifiedElement]<br>&nbsp;&nbsp;&nbsp; s = 3 [IntElement]<br>&gt;&gt;&gt;<br></p><p>&nbsp;</p><p>vs.</p><p>&nbsp;</p><p>&gt;&gt;&gt; root = objectify.fromstring("""<br>... &lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;<br>... &lt;s xsi:type="xsd:string"&gt;3&lt;/s&gt;<br>... &lt;/root&gt;""")<br>&gt;&gt;&gt; print objectify.dump(root)<br>root = None [ObjectifiedElement]<br>&nbsp;&nbsp;&nbsp; s = '3' [StringElement]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * xsi:type = 'xsd:string'<br>&gt;&gt;&gt;</p><p>&nbsp;</p><p>HTH,</p><p>Holger&nbsp;</p><p>&nbsp;</p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><div class="signature"><br /><br /><br />-- <br />Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! <br />Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer</div></body></html>