Hello,
A couple more newbie questions:
1. Using findall or xpath, what's the difference between "//" and ".//"?
I see both in examples.
https://www.w3schools.com/xml/xpath_syntax.asp
2. Why does the following script fail finding the elements I wish to
remove from the tree?
********************* SCRIPT
import lxml.etree as et
from lxml import html
import sys
#Exit if no input file on command line
arguments = len(sys.argv) - 1
if arguments != 1:
print ("Must pass one KML file")
sys.exit()
inputfile = sys.argv[1]
parser = et.XMLParser(remove_blank_text=True)
tree = et.parse(inputfile,parser)
root = tree.getroot()
#=========== Remove namespace prefixes
#https://stackoverflow.com/questions/60486563/remove-namespace-from-xml-with-comment-python
for elem in root.getiterator():
# For elements, replace qualified name with localname
if not(type(elem) == et._Comment):
elem.tag = et.QName(elem).localname
# Remove attributes that are in a namespace
for attr in elem.attrib:
if "{" in attr:
elem.attrib.pop(attr)
# Remove unused namespace declarations
et.cleanup_namespaces(root)
#=========== Remove blocs/elements: snippet, LookAt, Style, StyleMap
#https://stackoverflow.com/questions/22560862/elementtree-findall-or-operator
tracks = root.findall('.//snipet|.//LookAt|.//Style|.//StyleMap')
if len(tracks):
print("Elements found: snippet, LookAt, Style, StyleMap")
#remove elements
else:
print("No elements found: snippet, LookAt, Style, StyleMap")
#print(et.dump(root))
Here's the input data:
********************* DATA
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
<snippet>Created lun. août 30 21:02:41 2021</snippet>
<LookAt>
<longitude>1.151909</longitude>
<latitude>44.789630</latitude>
<range>322203.100706</range>
</LookAt>
<Style id="track_n">
<IconStyle>
<scale>.5</scale>
<Icon>
<href>https://earth.google.com/images/kml-icons/track-directional/track-none.png</href>
</Icon>
</IconStyle>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
</Style>
<StyleMap id="track">
<Pair>
<key>normal</key>
<styleUrl>#track_n</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#track_h</styleUrl>
</Pair>
</StyleMap>
<Folder>
<name>Tracks</name>
<Folder>
<name>Some track</name>
<snippet/>
<Placemark>
<name>Path</name>
<styleUrl>#lineStyle</styleUrl>
<LineString>
<coordinates>
0.156119,45.649872,101.25
0.156389,45.649822,100.25
</coordinates>
</LineString>
</Placemark>
</Folder>
</Folder>
</Document>
</kml>
Thank you.
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com