简体   繁体   English

lxml.objectify 和前导零

[英]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 :当 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

From what I understand, objectify automatically makes the b element an IntElement class instance.据我了解, objectify自动使b元素成为IntElement类实例。 But, it also does that even if I try to explicitly set the type with an XSD schema :但是,即使我尝试使用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)

Prints:印刷:

1
<class 'lxml.objectify.IntElement'>
01

How can I force objectify to recognize this b element as a string element?如何强制objectify将此b元素识别为字符串元素?

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.根据文档和观察到的行为,似乎XSD Schema仅用于验证,但不参与确定属性数据类型的过程。

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 :例如,当一个元素在 XSD 中被声明为integer类型,但 XML 中的实际元素的值为x01 ,元素无效异常会正确引发:

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 Schema xsi:type (链接部分中的第 4 号)提到了关于如何匹配数据类型的objectify文档,但那里的示例代码表明这意味着直接在实际的 XML 元素中添加xsi:type属性,而不是通过单独的 XSD 文件,例如:

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)  # 01
print(type(a.b)) # <type 'lxml.objectify.StringElement'>
print(a.b.text) # 01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM