首页 > 解决方案 > 从 Python 中的 XSD 文件中提取带有文档的枚举

问题描述

我正在尝试编写一个函数来从 XSD 文件中获取一些值的描述,其结构如下

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xs:element name="File">
        <xs:annotation>
            <xs:documentation>List of centers</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="Register">
                    <xs:annotation>
                        <xs:documentation>Center list registers</xs:documentation>
                    </xs:annotation>
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="CenterType">
                                <xs:annotation>
                                    <xs:documentation>Type of center  </xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:int">
                                        <xs:totalDigits value="1"/>
                                        <xs:enumeration value="1">
                                            <xs:annotation>
                                                <xs:documentation>Own center</xs:documentation>
                                            </xs:annotation>
                                        </xs:enumeration>
                                        <xs:enumeration value="2">
                                            <xs:annotation>
                                                <xs:documentation>External center</xs:documentation>
                                            </xs:annotation>
                                        </xs:enumeration>
                                        <xs:enumeration value="3">
                                            <xs:annotation>
                                                <xs:documentation>Associated center</xs:documentation>
                                            </xs:annotation>
                                        </xs:enumeration>
                                        <xs:enumeration value="4">
                                            <xs:annotation>
                                                <xs:documentation>Other</xs:documentation>
                                            </xs:annotation>
                                        </xs:enumeration>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

例如,如果我把

get_value("CenterType", "1")

我的函数可能会返回“自己的中心”

我正在使用带有 XMLSchema 的 Python 3.8。

我写了这个片段,我可以打印所有元素的标签

xsd_xml = xmlschema.XMLSchema(xsd_file)     
fichero = xsd_xml.elements["File"][0]
            
for elem in fichero:
    print(elem.tag)

但我需要访问枚举和文档字段。我怎样才能提取这些数据?

标签: python-3.xxsdxmlschema

解决方案


使用解开

import untangle

xsd_file = "C:\\code\\python\\xsd\\test.xsd"
obj = untangle.parse(xsd_file)

res = obj.xs_schema.xs_element.xs_complexType.xs_sequence.xs_element.xs_complexType.xs_sequence.xs_element.xs_simpleType.xs_restriction.xs_enumeration

推荐阅读