当在控制台上打印 objectify 元素时,前导零会丢失,但会保留在.text
:
>>> from lxml import objectify
>>>
>>> xml = "<a><b>01</b></a>"
>>> a = objectify.fromstring(xml)
>>> print(a.b)
1
>>> print(a.b.text)
01
据我了解,objectify
自动使b
元素成为IntElement
类实例。但是,即使我尝试使用XSD 模式显式设置类型,它也会这样做:
from io import StringIO
from lxml import etree, objectify
f = StringIO('''
<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:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
''')
schema = etree.XMLSchema(file=f)
parser = objectify.makeparser(schema=schema)
xml = "<a><b>01</b></a>"
a = objectify.fromstring(xml, parser)
print(a.b)
print(type(a.b))
print(a.b.text)
印刷:
1
<class 'lxml.objectify.IntElement'>
01
如何强制objectify
将此元素识别b
为字符串元素?