[lxml-dev] About objectify
jholg at gmx.de
jholg at gmx.de
Tue Jan 22 14:54:42 CET 2008
Hi David,
> Does anyone have a simple example with a short XML file containing some
> elements, its schema, and the python code parsing the file using
> objectify with the xsi namespace to do type association ?
>
>
I'm not quite sure I understand what you try to achieve,
but lxml.objectify does not necessarily need a schema:
$ cat simpleInstance.xml
<root>
<s>A string, hopefully.</s>
</root>
You can simply parse this using objecify:
>>> from lxml import etree, objectify
>>> root = objectify.parse("simpleInstance.xml").getroot()
>>> print objectify.dump(root)
root = None [ObjectifiedElement]
s = 'A string, hopefully.' [StringElement]
>>> print root.s
A string, hopefully.
>>>
What you can do is validate this XML tree against a schema:
$ cat simpleSchema.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root" type="RootType"/>
<xsd:complexType name="RootType">
<xsd:sequence>
<xsd:element name="s" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
>>> from lxml import etree, objectify
>>> root = objectify.parse("simpleInstance.xml").getroot()
>>> schema = etree.XMLSchema(objectify.parse("simpleSchema.xsd"))
>>> schema.validate(root)
True
>>>
No xsi:type information anywhere, so far.
What you currently can not do is use the schema to *add* xsi:type
attributes
to the XML instance. Or, to put it another way, schema-validation does not
add any type-information.
No problem, though, if the XML contains xsi:type information:
>>> from lxml import etree, objectify
>>> root = objectify.parse("simpleInstance2.xml").getroot()
>>> print objectify.dump(root)
root = None [ObjectifiedElement]
s = 'A string, hopefully.' [StringElement]
* xsi:type = 'xsd:normalizedString'
>>> print root.s
A string, hopefully.
>>> schema = etree.XMLSchema(objectify.parse("simpleSchema.xsd"))
>>> schema.validate(root)
True
>>>
If xsi:type information is available, it will be used to determine the
lxml.objectify
type representation of an element:
Consider
>>> root = objectify.fromstring("<root><s>3</s></root>")
>>> print objectify.dump(root)
root = None [ObjectifiedElement]
s = 3 [IntElement]
>>>
vs.
>>> root = objectify.fromstring("""
... <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
... xmlns:xsd="http://www.w3.org/2001/XMLSchema">
... <s xsi:type="xsd:string">3</s>
... </root>""")
>>> print objectify.dump(root)
root = None [ObjectifiedElement]
s = '3' [StringElement]
* xsi:type = 'xsd:string'
>>>
HTH,
Holger
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codespeak.net/pipermail/lxml-dev/attachments/20080122/eb5c9536/attachment.htm
More information about the lxml-dev
mailing list