lxml:objectify: data type of attribute
is there any way to specify the data type of an attribute? Seems like its always string. -- ambiHome GmbH Theaterstr. 19 52062 Aachen Tel +49-241-5380948-3 Fax +49-241-5380948-9 info@ambiHome.de Gesellschaft mit beschränkter Haftung Amtsgericht Aachen, HRB 15813 USt-IdNr. DE268539526 Geschäftsführung: Dipl.-Ing. Roman Wienert
Hi,
Gesendet: Dienstag, 22. Oktober 2013 um 15:38 Uhr Von: "Sebastian Fey, ambiHome GmbH" <fey@ambihome.de> An: lxml@lxml.de Betreff: [lxml] lxml:objectify: data type of attribute
is there any way to specify the data type of an attribute? Seems like its always string.
Yes, attributes are just (unicode) strings. Unlike lxml.objectify elements which behave like Python builtin datatypes for most practical applications. lxml elements are basically Python proxy elements representing the underlying XML tree (text). If you read up on how objectify matches datatypes (http://lxml.de/objectify.html#how-data-types-are-matched) you'll notice the use of certain type annotation attributes that are relevant to the type lookup. This is e.g. relevant if you do s.th. like
tree.x = "0" Without py:pytype or xsi:type annotation you'd lose the information that x is supposed to be a string and not an integer in a serialize/parse cycle (or even if the x proxy element goes out of scope and is looked up again in the tree at some later point).
As XML attributes can't themselves carry attributes, as opposed to elements, this scheme isn't directly applicable to attributes. Hence, those are just strings. That said, you may be able to hook custom element classes into lxml that provide you with a similar type lookup scheme for the elem.get()/set()/attrib() stuff, if you don't care about elem.set('attr_a', '5') being returned as elem.get('attr_a') -> 5 [int] or the like. lxml.objectify's classes are themselves just (lxml-batteries-included ;-)) custom element classes. (Haven't tried it out, see http://lxml.de/element_classes.html for background) Holger
participants (2)
-
jholg@gmx.de
-
Sebastian Fey, ambiHome GmbH