簡體   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