lxml.objectify和前导零 [英] lxml.objectify and leading zeros

查看:114
本文介绍了lxml.objectify和前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制台上打印objectify元素时,前导零会丢失,但会保留在.text中:

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
>>> 
>>> xml = "<a><b>01</b></a>"
>>> a = objectify.fromstring(xml)
>>> print(a.b)
1
>>> print(a.b.text)
01

据我了解,objectify自动使b元素成为IntElement类实例.但是,即使我尝试使用 XSD架构显式设置类型,它也能做到这一点 a>:

From what I understand, objectify automatically makes the b element an IntElement class instance. But, it also does that even if I try to explicitly set the type with an 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元素识别为字符串元素?

How can I force objectify to recognize this b element as a string element?

推荐答案

根据文档和观察到的行为,看来XSD Schema仅用于验证,但没有涉及在确定属性数据类型的过程中.

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中声明为类型integer,但是XML中的实际元素的值为x01时,将正确引发元素无效异常:

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'.


尽管objectify文档有关数据类型如何匹配提到了 XML Schema xsi:type (链接部分中的第4号),其中的示例代码建议通过直接在实际XML元素中添加xsi:type属性来表示 ,而不是通过单独的XSD文件,例如:


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

这篇关于lxml.objectify和前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆