python-3.x - 从 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)
但我需要访问枚举和文档字段。我怎样才能提取这些数据?
解决方案
使用解开
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
推荐阅读
- php - 如何在 Eloquent 中将“或”与多个“where”一起使用?
- excel - 根据 2 个条件查找值
- javascript - 重定向没有传递“req”
- amazon-web-services - 如何使用 golang 在另一个 Lambda 函数中调用 Lambda 函数
- sqlite - SQLite DELETE 条件,Firefox 历史
- mysql - 如何正确解决 mySQL 上的 ERROR 1044?这里的其他答案不能解决问题
- android - 发送当前选择的图像槽意图的图像随机化器
- c++ - 在c ++中基于范围的for循环中使用引用作为控制变量
- java - 有效计算两个向量之间的差异
- ios - 调用原始方法时,Xamarin 方法在设备上调用失败