首页 > 解决方案 > 如何从python中的xsd文件中递归提取所有元素

问题描述

我有一个 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>

我可以通过运行以下代码来提取所有名称:

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']

我想得到更像分层输出。例如,在它获得 ref abc.efg_MatchHandlingCharacteristics 后,它应该先打印 abc.efg_CharCd 和 CharText,然后再打印 abc.efg_MatchScoreReport。

标签: pythonxmlxsdxml-parsing

解决方案


推荐阅读