Skip to content Skip to sidebar Skip to footer

Lxml.objectify And Leading Zeros

When the objectify element is printed on the console, the leading zero is lost, but it is preserved in the .text: >>> from lxml import objectify >>> >>>

Solution 1:

Based on the documentation and the behavior observed, it seems that XSD Schema is only used for validation, but isn't involved in the process of determining property data type whatsoever.

For example, when an element is declared to be of type integer in the XSD, but then the actual element in the XML has value of x01, element invalid exception is correctly raised :

f = StringIO(u'''
   <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <xsd:element name="a" type="AType"/>
     <xsd:complexType name="AType">
       <xsd:sequence>
         <xsd:element name="b" type="xsd:integer" />
       </xsd:sequence>
     </xsd:complexType>
   </xsd:schema>
 ''')
schema = etree.XMLSchema(file=f)
parser = objectify.makeparser(schema=schema)

xml = '''<a><b>x01</b></a>'''a = objectify.fromstring(xml, parser)
# the following exception raised:# lxml.etree.XMLSyntaxError: Element 'b': 'x01' is not a valid value of....# ...the atomic type 'xs:integer'.

Despite objectify documentation on how data types are matched mentioned about XML Schema xsi:type (no. 4 in the linked section), the example code there suggests that it means by adding xsi:type attribute directly in the actual XML element, not via a separate XSD file, for example :

xml = '''
<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <b xsi:type="string">01</b>
</a>
'''
a = objectify.fromstring(xml)

print(a.b)  # 01print(type(a.b)) # <type 'lxml.objectify.StringElement'>print(a.b.text) # 01

Post a Comment for "Lxml.objectify And Leading Zeros"