如何在python中从xsd文件中递归提取所有元素

如何在python中从xsd文件中递归提取所有元素,python,xml,xsd,xml-parsing,Python,Xml,Xsd,Xml Parsing,我有一个XSD文件,我想从中递归提取所有元素——基本上,我需要保存列表中所有元素的路径。我是新来的,所以请原谅我,但我尝试过使用xmltree,但结果并没有达到预期效果 import xml.etree.ElementTree as ET tree = ET.parse('/in_xml.xsd') root = tree.getroot() d = [] for items in root.getiterator(): if items.tag not in d: d.

我有一个XSD文件,我想从中递归提取所有元素——基本上,我需要保存列表中所有元素的路径。我是新来的,所以请原谅我,但我尝试过使用xmltree,但结果并没有达到预期效果

import xml.etree.ElementTree as ET
tree = ET.parse('/in_xml.xsd')
root = tree.getroot()
d = []
for items in root.getiterator():
    if items.tag not in d:
        d.append(items.tag)
有人能帮忙吗?谢谢

更新:

我的XSD文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:element name="abc.efg.MatchScoringInfo">
                <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="abc.efg_MatchDirectorScore" type="String" minOccurs="0"/>
                                <xs:element name="abc.efg_MatchInd" type="String" minOccurs="0"/>
                                <xs:element name="abc.efg_MatchFoundInd" type="String" minOccurs="0"/>
                                <xs:element name="abc.efg_EmailInd" type="String" minOccurs="0"/>
                                <xs:element name="abc.efg_MatchTimesScored" type="String" minOccurs="0"/>
                                <xs:element ref="abc.efg_MatchHandlingCharacteristics" minOccurs="0" maxOccurs="unbounded"/>
                                <xs:element ref="abc.efg_MatchRules" minOccurs="0" maxOccurs="unbounded"/>
                                <xs:element name="abc.efg_MatchScoreReport" minOccurs="0" maxOccurs="unbounded">
                                        <xs:complexType>
                                                <xs:sequence minOccurs="0">
                                                        <xs:element name="abc.efg_RawScore" minOccurs="0"/>
                                                        <xs:element name="abc.efg_AdjustedScore" minOccurs="0"/>
                                                        <xs:element name="NumMatches" type="String" minOccurs="0"/>
                                                        <xs:element name="abc.efg_SearchableMatchCd" type="StringCd" minOccurs="0" maxOccurs="unbounded"/>
                                                        <xs:element ref="abc.efg_ScoredMatch" minOccurs="0" maxOccurs="unbounded"/>
                                                </xs:sequence>
                                                <xs:attribute name="id" type="xs:ID" use="optional"/>
                                                <xs:attribute name="idref" type="xs:string" use="optional"/>
                                        </xs:complexType>
                                </xs:element>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:ID" use="optional"/>
                </xs:complexType>
        </xs:element>
        <xs:element name="abc.efg_MatchHandlingCharacteristics">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element name="abc.efg_CharCd" type="StringCd" minOccurs="0"/>
                                <xs:element name="abc.efg_CharText" type="String" minOccurs="0"/>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:ID" use="optional"/>
                </xs:complexType>
        </xs:element>
</xs:schema>

我想得到更像层次化的输出。例如,在获得ref abc.efg_MatchHandlingCharacteristics后,应首先打印abc.efg_CharCd和CharText,然后打印abc.efg_MatchScoreReport

我猜您的意思是希望获得所有元素声明(对应于xs:element元素)?您是只想要全局元素声明,还是想要局部元素声明?关于每个元素声明,您希望提取哪些信息?是否需要遵循xs:include/xs:import声明?需要更多关于需求的细节:至少,给出示例输入和所需输出。用输入和我要找的内容更新问题。谢谢。
from lxml import etree
t3 = etree.ElementTree(file='/myxsd.xsd')
name3 = t3.xpath("//xsd:element/@name", namespaces={"xsd": "http://www.w3.org/2001/XMLSchema"})

Output:
['abc.efg.MatchScoringInfo',
 'abc.efg_MatchDirectorScore',
 'abc.efg_MatchInd',
 'abc.efg_MatchFoundInd',
 'abc.efg_EmailInd',
 'abc.efg_MatchTimesScored',
 'abc.efg_MatchScoreReport',
 'abc.efg_RawScore',
 'abc.efg_AdjustedScore',
 'NumMatches',
 'abc.efg_SearchableMatchCd',
 'abc.efg_MatchHandlingCharacteristics',
 'abc.efg_CharCd',
 'abc.efg_CharText']