python - 如何从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。
解决方案
推荐阅读
- html - 在 html 选择中悬停或选择项目时,如何避免更改背景颜色?
- asp.net-mvc - IdentityModel vs Microsoft.IdentityModel vs System.IdentityModel
- flutter - Dart/Flutter - 仅保留列表中的重复项
- javascript - 如何切换 Blockly 工具箱
- azure-devops - 是否有 Azure devops rest API 来管理进程级别的安全性
- javascript - 将 React 类重构为钩子 - 实体更新组件
- javascript - 我尝试创建导航栏,但我的 javascript 代码不起作用
- r - 试图理解 R Scales label_percent() 的结果
- nginx - Nginx 使用 CORS 和凭据
- c++ - 无法删除模板类中 stack.top() 指向的对象