我的 XML 文件结构是:
<tp:Package xml:lang='en-US' xmlns:tp='http://myorg.org/2016/mypackage'>
<tp:identifier>http://www.myweb.com/</tp:identifier>
<tp:name>MyName</tp:name>
<tp:description xml:lang='en-US'>My Description</tp:description>
<tp:version>2020-01-01</tp:version>
<tp:license href='http://www.myweb.com/terms/TermsConditions.html' name='Terms and Conditions' />
<tp:publisher>MyPublisher</tp:publisher>
<tp:publisherURL>http://www.mypublisherurl.com/</tp:publisherURL>
<tp:publisherCountry>US</tp:publisherCountry>
<tp:publicationDate>2020-01-01</tp:publicationDate>
<tp:entryPoints>
<tp:entryPoint>
<tp:name>Form A</tp:name>
<tp:description>This is Form A.</tp:description>
<tp:version>v313</tp:version>
<tp:entryPointDocument href='http://www.myweb.com/myfile.xsd' />
<tp:formType>1</tp:formType>
</tp:entryPoint>
<tp:entryPoint>
<tp:name>Form B</tp:name>
<tp:description>This is Form B.</tp:description>
<tp:version>v313</tp:version>
<tp:entryPointDocument href='http://www.myweb.com/myfile.xsd' />
<tp:formType>2</tp:formType>
</tp:entryPoint>
</tp:entryPoints>
</tp:Package>
如何使用 etree 读取此文件并遍历每个文件标记并打印元素tp:name、tp:description、tp:version、tp:entryPointDocument、tp:formType 的值
以下是我的部分python代码:
from lxml import etree
tree = etree.parse(xmlfilepath)
root = tree.getroot()
for elt in root.xpath("//tp:entryPoints", namespaces={'tp': 'http://myorg.org/2016/mypackage'}):
print(elt)