[lxml-dev] About objectify
David Soulayrol
dsoulayrol at free.fr
Fri Jan 25 11:00:41 CET 2008
Le mercredi 23 janvier 2008 à 08:45 +0100, jholg at gmx.de a écrit :
> Hi,
>
> > - Why is the root element still an ObjectifiedElement instance ? It
> > seems to me I applied the same rules for both of my defined types.
>
>
> Now, objectify bases its element class lookup (i.e. which element
> class to use for the Python proxy representation) on certain rules:
>
> 1. if element has children => no data class
> 2. if element is defined as xsi:nil, return NoneElement class
> 3. check for Python type hint
> 4. check for XML Schema type hint
> 5. guess element class
Yes, it is in documentation which I should have read with more
attention. Sorry for that.
> # maps attribute values to element classes
> xsitype_class_mapping = {
> "site": Configuration,
> "title": MyString,
> }
>
> lookup = etree.AttributeBasedElementClassLookup(
> "{http://www.w3.org/2001/XMLSchema-instance}type",
> xsitype_class_mapping,
> objectify.ObjectifyElementClassLookup())
>
> parser = etree.XMLParser()
> parser.setElementClassLookup(lookup)
> objectify.setDefaultParser(parser)
>
> root = objectify.fromstring("""
> <site xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
> xsi:type='site'>
>
> <title xsi:type='title'>Achille 2.0</title>
> <value xsi:type='long'>2</value>
> </site>
> """)
>
> print objectify.dump(root)
This makes sense now.
> > - Is there a way to specify the xsi:type in a schema sheet ? This
> > question may sound stupid, but I'm still learning the XSD spec, and
> > I wonder if objectify could rely entirely on the schema, without the
> > need to add anything in the XML document itself.
> For now, there is nothing like a "typifier" that takes an instance and
> a schema and adds type information from the schema to the instance
> document.
Thanks for all the help.
--
David
More information about the lxml-dev
mailing list