[英]How to get xml elements which have childs with a certain tag and attribute
我想找到具有某些子元素的 xml 元素。 子元素需要有一個給定的標簽和一個設置為特定值的屬性。
舉一個具體的例子(根據官方文檔)。 我想找到所有具有屬性name="Austria"
的子元素neighbor
的country
元素:
import xml.etree.ElementTree as ET
data = """<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<neighbor name="Malaysia" direction="N"/>
<partner name="Austria"/>
</country>
<country name="Panama">
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
"""
root = ET.fromstring(data)
我沒有成功的嘗試:
countries1 = root.findall('.//country[neighbor@name="Austria"]')
countries2 = root.findall('.//country[neighbor][@name="Austria"]')
countries3 = root.findall('.//country[neighbor[@name="Austria"]]')
這都給了:
SyntaxError:無效謂詞
以下解決方案顯然是錯誤的,因為發現了太多元素:
countries4 = root.findall('.//country/*[@name="Austria"]')
countries5 = root.findall('.//country/[neighbor]')
其中countries4
包含所有具有屬性name="Austria"
的元素,但包括partner
元素。 countries5
包含所有具有任何相鄰元素作為子元素的元素。
我想找到所有具有屬性 name="Austria" 的子元素鄰居的國家元素
見下文
import xml.etree.ElementTree as ET
data = """<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<neighbor name="Malaysia" direction="N"/>
<partner name="Austria"/>
</country>
<country name="Panama">
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
"""
root = ET.fromstring(data)
countries_with_austria_as_neighbor = [c.attrib['name'] for c in root.findall('.//country') if
'Austria' in [n.attrib['name'] for n in c.findall('neighbor')]]
print(countries_with_austria_as_neighbor)
output
['Liechtenstein']
import xml.etree.ElementTree as ET
data = """<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<neighbor name="Malaysia" direction="N"/>
<partner name="Austria"/>
</country>
<country name="Panama">
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
<country name="Liechtenstein">
<neighbor name="Austria" direction="dummy"/>
<neighbor name="Switzerland" direction="W"/>
</country>
</data>
"""
root = ET.fromstring(data)
for x in root.findall(".//country/neighbor[@name='Austria']"):
print(x.attrib)
Output:
{'name': 'Austria', 'direction': 'E'}
{'name': 'Austria', 'direction': 'dummy'}
//
: Selects all subelements, on all levels beneath the current element. For example, .//egg selects all egg elements in the entire tree.
Selects all subelements, on all levels beneath the current element. For example, .//egg selects all egg elements in the entire tree.
[@attrib='value']
: Selects all elements for which the given attribute has the given value. The value cannot contain quotes
Selects all elements for which the given attribute has the given value. The value cannot contain quotes
for x in root.find('.'):
if x[0].attrib['name'] == 'Austria':
print(x.attrib['name'])
Output: Liechtenstein
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.