使用名称空间和 Python (lxml) 从 XML 获取 XML 属性
Getting XML attributes from XML with namespaces and Python (lxml)
我正在尝试从下面的 XML 中获取 "id" 和 "href" 属性。到目前为止,我似乎无法理解命名空间方面的问题。使用没有名称空间引用的 XML 我可以很容易地得到东西。但这让我很困惑。任何想法将不胜感激!
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns3:searchResult total="1" xmlns:ns5="ers.ise.cisco.com" xmlns:ers-v2="ers- v2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns3="v2.ers.ise.cisco.com">
<ns3:resources>
<ns5:resource id="d28b5080-587a-11e8-b043-d8b1906198a4"name="00:1B:4F:32:27:50">
<link rel="self" href="https://ho-lab-ise1:9060/ers/config/endpoint/d28b5080-587a-11e8-b043-d8b1906198a4"type="application/xml"/>
</ns5:resource>
</ns3:resources>
您可以使用xpath
函数搜索所有资源并对其进行迭代。该函数有一个 namespaces 关键字参数。可以用它来声明命名空间前缀和命名空间URL.
之间的映射
想法是这样的:
from lxml import etree
NS = {
"ns5": "ers.ise.cisco.com",
"ns3": "v2.ers.ise.cisco.com"
}
tree = etree.parse('your.xml')
resources = tree.xpath('//ns5:resource', namespaces=NS)
for resource in resources:
print(resource.attrib['id'])
links = resource.xpath('link')
for link in links:
print(link.attrib['href'])
抱歉,这个没有测试
@laurent-laporte 的回答非常适合展示如何处理多个名称空间 (+1)。
但是,如果您真的只需要 select 几个属性,无论它们在什么命名空间中,您都可以在谓词中测试 local-name()
...
from lxml import etree
tree = etree.parse('your.xml')
attrs = tree.xpath("//@*[local-name()='id' or local-name()='href']")
for attr in attrs:
print(attr)
这将打印(与 Laurent 的相同)...
d28b5080-587a-11e8-b043-d8b1906198a4
https://ho-lab-ise1:9060/ers/config/endpoint/d28b5080-587a-11e8-b043-d8b1906198a4
我正在尝试从下面的 XML 中获取 "id" 和 "href" 属性。到目前为止,我似乎无法理解命名空间方面的问题。使用没有名称空间引用的 XML 我可以很容易地得到东西。但这让我很困惑。任何想法将不胜感激!
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns3:searchResult total="1" xmlns:ns5="ers.ise.cisco.com" xmlns:ers-v2="ers- v2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns3="v2.ers.ise.cisco.com">
<ns3:resources>
<ns5:resource id="d28b5080-587a-11e8-b043-d8b1906198a4"name="00:1B:4F:32:27:50">
<link rel="self" href="https://ho-lab-ise1:9060/ers/config/endpoint/d28b5080-587a-11e8-b043-d8b1906198a4"type="application/xml"/>
</ns5:resource>
</ns3:resources>
您可以使用xpath
函数搜索所有资源并对其进行迭代。该函数有一个 namespaces 关键字参数。可以用它来声明命名空间前缀和命名空间URL.
想法是这样的:
from lxml import etree
NS = {
"ns5": "ers.ise.cisco.com",
"ns3": "v2.ers.ise.cisco.com"
}
tree = etree.parse('your.xml')
resources = tree.xpath('//ns5:resource', namespaces=NS)
for resource in resources:
print(resource.attrib['id'])
links = resource.xpath('link')
for link in links:
print(link.attrib['href'])
抱歉,这个没有测试
@laurent-laporte 的回答非常适合展示如何处理多个名称空间 (+1)。
但是,如果您真的只需要 select 几个属性,无论它们在什么命名空间中,您都可以在谓词中测试 local-name()
...
from lxml import etree
tree = etree.parse('your.xml')
attrs = tree.xpath("//@*[local-name()='id' or local-name()='href']")
for attr in attrs:
print(attr)
这将打印(与 Laurent 的相同)...
d28b5080-587a-11e8-b043-d8b1906198a4
https://ho-lab-ise1:9060/ers/config/endpoint/d28b5080-587a-11e8-b043-d8b1906198a4