【问题标题】:XMLParser skips attributes while parsing XML Schema fileXMLParser 在解析 XML Schema 文件时跳过属性
【发布时间】:2019-09-26 08:31:51
【问题描述】:

我需要阅读 XML 架构文件并仅提取这些元素,它们具有字段 minOccurs="0"。但是我面临的问题是,当 XML Parser 在解析文档时跳过该字段时。

就像我在下面的代码中向您展示的那样。

我有一个示例 XML 文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:include schemaLocation="def.xml"/>
    <xsd:element name="MainElementName">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="A">
                    <xsd:complexType>
                        <xsd:attribute name="AA" required="False" type="string"/>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="B" minOccurs="0" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:attribute name="BA" type="string"/>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

然后我用这段代码解析它:

    with open(xsd_path, 'r'):
        try:
            parser = et.XMLParser(remove_blank_text=True)
            tree = et.parse(xsd_path, parser)
            tmp_text = et.tostring(tree, pretty_print=True, encoding=str)
        except IOError as e:
            print(e)

我得到了输出:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:include schemaLocation="def.xml"/>
    <xsd:element name="MainElementName">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="A">
                    <xsd:complexType>
                        <xsd:attribute name="AA" type="string"/>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="B">
                    <xsd:complexType>
                        <xsd:attribute name="BA" type="string"/>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

我不知道为什么解析器会跳过属性中的required 字段和元素中的min/maxOccurs。有谁知道如何解决这个问题?

【问题讨论】:

    标签: python xsd lxml


    【解决方案1】:

    您的代码不正确,我无法编译。例如,ElementTree.tostring() 采用 Element 实例,但您传递的是 ElementTree 实例 (tree)。

    此代码适用于我:

    import xml.etree.ElementTree as et
    
    parser = et.XMLParser()
    tree = et.parse('/path/to.xml', parser)
    tmp_text = et.tostring(tree.getroot(), encoding='unicode')
    print(tmp_text)
    

    使用 Python 3.6.8 的输出是:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:include schemaLocation="def.xml" />
        <xs:element name="MainElementName">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="A">
                        <xs:complexType>
                            <xs:attribute name="AA" required="False" type="string" />
                        </xs:complexType>
                    </xs:element>
                    <xs:element maxOccurs="unbounded" minOccurs="0" name="B">
                        <xs:complexType>
                            <xs:attribute name="BA" type="string" />
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    【讨论】:

    • OP 很可能使用 lxml,它接受 ElementTree 实例作为 tostring() 的参数。
    • 是的,我使用的是 lxml ......这是错误的。非常感谢
    • 到底是什么错误?这个答案没有解释为什么“解析器跳过属性中的必填字段和元素中的 min/maxOccurs”。
    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多