繁体   English   中英

为什么这在Python2.7中有效但在Python3.5中无效?

[英]Why does this work in Python2.7 but not in Python3.5?

我有以下代码针对XSD验证XML文档:

import lxml
import lxml.etree

xsd_definition = lxml.etree.XML('''\
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                elementFormDefault="qualified">
        <xs:element name="test" type="testType"/>
        <xs:complexType name="testType">
            <xs:sequence>
                <xs:element name="testElement" type="testElementType"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="testElementType">
            <xs:attribute name="intAttr" type="xs:positiveInteger" use="required"/>
        </xs:complexType>
    </xs:schema>
''')
xsd = lxml.etree.XMLSchema(xsd_definition)
parser = lxml.etree.XMLParser(schema=xsd)

test_xml = '''\
    <test>
        <testElement intAttr="10"/>
    </test>
'''
lxml.etree.fromstring(test_xml, parser)

这适用于Python2.7,但不适用于Python3.5。 在Python3.5中,出现以下异常:

lxml.etree.XMLSyntaxError: Element 'testElement', attribute 'intAttr': '' is not a valid value of the atomic type 'xs:positiveInteger'.

似乎解析器没有读取“ testElement”元素的“ intAttr”属性的“ 10”值。 有人知道我在做什么错吗?

显然问题出在我使用的lxml版本。 我与python 2.7一起使用的lxml版本是3.7,而我与python 3.5一起使用的版本是3.8。 这种差异的原因是因为我使用Debian的lxml软件包为python 2.7安装了lxml,而我使用了pip为python 3.5安装了lxml。 一旦我将python 3.5降到3.7,问题就消失了。

暂无
暂无

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

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