我正在解析和XSD尝试获取一个唯一的属性列表。 XSD的格式是......
<xsd:ComplexType name=main_value>
<xsd:choice>
<xsd:element name=value1 />
<xsd:element name=value2 />
<xsd:element name=value3 />
</xsd:choice>
</xsd:ComplexType>
<xsd:ComplextType name=value1 >
<xsd:all>
<element maxOccurs=1 minOccurs=1 name="Attribute1">
<element maxOccurs=1 minOccurs=0 name="Attribute2">
<element maxOccurs=1 minOccurs=0 name="Attribute3">
</xsd:all>
</xsd:complexType>
顶级complexType是complexTypes的列表。例如,顶级ComplexType可能类似于'Animal',它会列出子类别complexTypes:AnimalFood,AnimalAccessories等。比子类别complexTypes列出其下面的属性,即shortDescription,keyFeatures,brand,等等
目前,我正在利用这些元素并提取其属性。
for child in document_tree.getroot():
for grandchild in child:
for greatgrandchild in grandchild:
if not dict(greatgrandchild.attrib.items()).get('type'):
attributes.append(dict(greatgrandchild.attrib.items()).get('name'))
我想尝试按元素类型过滤。例如,我需要的所有属性都嵌套在'all'元素中。有没有办法使用xml.etree.ElementTree,您可以通过某种属性类型过滤整个XSD / XML模式。
您可能需要某种访问者函数来递归搜索。
以下代码段对我来说是一个独立的示例:
import xml.etree.ElementTree as ET
data = """<myxsd><ComplexType name="main_value">
<choice>
<element name="value1" />
<element name="value2" />
<element name="value3" />
</choice>
</ComplexType>
<ComplextType name="value1">
<all>
<element maxOccurs="1" minOccurs="1" name="Attribute1" />
<element maxOccurs="1" minOccurs="0" name="Attribute2" />
<element maxOccurs="1" minOccurs="0" name="Attribute3" />
</all>
<a><b><c><d><e><f>
<element maxOccurs="1" minOccurs="1" name="Attribute4" />
<element maxOccurs="1" minOccurs="0" name="Attribute5" />
<element maxOccurs="1" minOccurs="0" name="Attribute6" />
</f></e></d></c></b></a>
</ComplextType></myxsd>"""
root = ET.fromstring(data)
attributes = []
def visit(node):
if node.tag == "element":
name = node.attrib.get("name")
if name is not None:
attributes.append(name)
for child in node:
visit(child)
visit(root)
print(attributes)