<!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> </p><p> $ cat simpleInstance.xml<br><root><br> <s>A string, hopefully.</s><br></root></p><p> </p><p>You can simply parse this using objecify:</p><p> </p>>>> from lxml import etree, objectify<br>>>> root = objectify.parse("simpleInstance.xml").getroot()<br>>>> print objectify.dump(root)<br>root = None [ObjectifiedElement]<br> s = 'A string, hopefully.' [StringElement]<br>>>> print root.s<br>A string, hopefully.<br>>>><br><p> </p><p>What you can do is validate this XML tree against a schema:</p><p> </p><p>$ cat simpleSchema.xsd<br><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><br> <br> <xsd:element name="root" type="RootType"/><br> <br> <xsd:complexType name="RootType"><br> <xsd:sequence><br> <xsd:element name="s" type="xsd:string"/><br> </xsd:sequence><br> </xsd:complexType><br></xsd:schema> <br></p><p> </p><p>>>> from lxml import etree, objectify<br>>>> root = objectify.parse("simpleInstance.xml").getroot()<br>>>> schema = etree.XMLSchema(objectify.parse("simpleSchema.xsd"))<br>>>> schema.validate(root)<br>True<br>>>> </p><p> </p><p>No xsi:type information anywhere, so far.</p><p> </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. </p><p> </p><p>No problem, though, if the XML contains xsi:type information:</p><p> </p><p>>>> from lxml import etree, objectify<br>>>> root = objectify.parse("simpleInstance2.xml").getroot()<br>>>> print objectify.dump(root)<br>root = None [ObjectifiedElement]<br> s = 'A string, hopefully.' [StringElement]<br> * xsi:type = 'xsd:normalizedString'<br>>>> print root.s<br>A string, hopefully.<br>>>> schema = etree.XMLSchema(objectify.parse("simpleSchema.xsd"))<br>>>> schema.validate(root)<br>True<br>>>><br></p><p> </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> </p><p>>>> root = objectify.fromstring("<root><s>3</s></root>")<br>>>> print objectify.dump(root)<br>root = None [ObjectifiedElement]<br> s = 3 [IntElement]<br>>>><br></p><p> </p><p>vs.</p><p> </p><p>>>> root = objectify.fromstring("""<br>... <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>... xmlns:xsd="http://www.w3.org/2001/XMLSchema"><br>... <s xsi:type="xsd:string">3</s><br>... </root>""")<br>>>> print objectify.dump(root)<br>root = None [ObjectifiedElement]<br> s = '3' [StringElement]<br> * xsi:type = 'xsd:string'<br>>>></p><p> </p><p>HTH,</p><p>Holger </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><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>