当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 schema 显式设置类型,它也会这样做。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
元素识别为字符串元素?