我有一个XSD文件,我想从递归中提取所有元素 - 基本上,我需要保存列表中所有元素的路径。我是新来的,请原谅我,但我尝试使用xmltree,它没有按预期工作。
cost_date
有人可以帮忙吗?谢谢!
更新:
我的XSD文件如下所示:
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)
我可以通过运行此代码来提取所有名称:
<?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。