I have a KML file which contains 2 placemarks :
Test1 and Test2.
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>Test1</name>
<styleUrl>style1</styleUrl>
<Point><coordinates>1,2</coordinates></Point>
<ExtendedData xmlns:mwm="https://example">
<mwm:visibility>1</mwm:visibility>
</ExtendedData>
</Placemark>
<Placemark>
<name>Test2</name>
<styleUrl>style2</styleUrl>
<Point><coordinates>3,4</coordinates></Point>
<ExtendedData xmlns:mwm="https://example">
<mwm:scale>19</mwm:scale>
<mwm:visibility>1</mwm:visibility>
</ExtendedData>
</Placemark>
</Document>
</kml>
Test2 has an element < mwm:scale > while Test1 does not.
My goal is to loop through all placemarks, and record in a list all placemarks' names and in another list all placemarks' scale.
I've been digging around lxml and Xpath option, but I can't find a way to have an "empty" output when the element (in this case "scale") doesn't exist in the Placemark (parent element).
This code :
import lxml.etree as et
tree = et.parse(file.kml)
for names in tree.xpath("/kml:kml/kml:Document/kml:Placemark/kml:name", namespaces={'kml': 'http://earth.google.com/kml/2.2','mwm': 'https://example'}):
name_list.append(names.text)
for scales in tree.xpath("/kml:kml/kml:Document/kml:Placemark/kml:ExtendedData/mwm:scale", namespaces={'kml': 'http://earth.google.com/kml/2.2','mwm': 'https://example'}):
scale_list.append(scales.text)
will give me those list
[Test1, Test2]
[19]
while I'm looking for a solution to get something like (if scale doesn't exist, output '0') :
[Test1, Test2]
[0, 19]
Any solution or idea ? I've been trying to iterate through the parsed XML but the 2 different namespaces (kml and mwm) make it impossible with the solutions I've find on the forum....
Thanks a lot for any help !
question from:
https://stackoverflow.com/questions/65906998/lxml-and-python-iterate-only-over-existing-elements 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…