python - Replace contents of lxml StringElement with starting a namespace war? -
I do not know how to change the contents of the lxml string element (in this case styleUrl) which already has a namespace (From other footpieces) I'm getting an element level namespace injection. This is a very distilled and simplified version that tries to describe only this issue to change a StyleMap name:
# / usr / bin / pykml import parser __future__ import print_function env dragon from import sys! lxml import kmlparser = "S-1" b4_et = kmlparser.parse ( 'b4.kml') b4_root = b4_et L = b4_root.xpath ( '// * [@ id = "% s"]' for frm = "lineStyle30218901714341461519022 "Objectify% frm [0] el.attrib ['id'] = to el = b4_root.xpath ('// * [text () =' #% s ']'% frm) [0] el.xpath ( '. /..')[0].styleUrl =' # '+ objectify.deannotate to (b4_root, xsi_nil = true) b4_et.write (sys.stdout, pretty_print = true) Test Data:
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Kml xmlns = "http://www.opengis.net/kml/2.2" xmlns: gx = "http://www.google.com/kml/ext/2.2" xmlns: kml = "http: // Www. Opengis.net/kml/2.2 "xmlns: atom =" http://www.w3.org/2005/ atom "& gt; & Lt; Document & gt; & Lt; Name & gt; Washing trails & lt; / Name & gt; & Lt; Open & gt; 1 & lt; / Open & gt; & Lt; Style id = "line style 30218901714341461519049" & gt; & Lt; LineStyle & gt; & Lt; Color & gt; Ff0080ff & lt; / Color & gt; & Lt; Width & gt; 4 & lt; / Width & gt; & Lt; / LineStyle & gt; & Lt; / Style & gt; & Lt; Style id = "line style 30218901714341461519027" & gt; & Lt; LineStyle & gt; & Lt; Color & gt; Ff0080ff & lt; / Color & gt; & Lt; Width & gt; 4 & lt; / Width & gt; & Lt; / LineStyle & gt; & Lt; / Style & gt; & Lt; Stylemap id = "line style 30218901714341461519022" & gt; & Lt; Pair & gt; & Lt; Key & gt; General & lt; / Key & gt; & Lt; StyleUrl & gt; # LineStyle30218901714341461519049 & lt; / StyleUrl & gt; & Lt; / Pair & gt; & Lt; Pair & gt; & Lt; Key & gt; Highlight & lt; / Key & gt; & Lt; StyleUrl & gt; # LineStyle30218901714341461519027 & lt; / StyleUrl & gt; & Lt; / Pair & gt; & Lt; / StyleMap & gt; & Lt; Placemark & gt; & Lt; Name & gt; Trail & lt; / Name & gt; & Lt; StyleUrl & gt; # LineStyle30218901714341461519022 & lt; / StyleUrl & gt; & Lt; LineString & gt; & Lt; Tessellate & gt; 1 & lt; / Tessellate & gt; & Lt; Coordinate & gt; -111.6472637672589,40.4810633294269,0 -111.650415221546,40.48116138407261,0 -111.6504410181637,40.48118694372887,0 & lt; / Coordinate & gt; & Lt; / LineString & gt; & Lt; / Placemark & gt; & Lt; / Documents & gt; & Lt; / Kml & gt; The only issue put a xmlns I have not been able to solve the lxml: py = "http://codespeak.net/lxml/objectify/pytype" The attribute in the newly created style Yule Element I'm guessing that this document is due to a default namespace for kml / 2.2. I do not know how to tell it to be new styleUrl pytype instead of kml ... & lt; StyleUrl xmlns:. Py = "http://codespeak.net/lxml/ objectify / pytype" & gt; # S1 & lt; / StyleUrl & gt; ...
Change the following:
el .xpath ( '. / ..') [0] .styleUrl = '#' + to with:
el.xpath ( '. /..')[0].styleUrl = objectify.StringElement (' # '+ to) Will give you whatever you want. But I'm not sure that this is the best way.
Biteedblu, you is set to set (key, value) method can use: el.set ('Id', to) # isntead's el.attrib ['id'] = to
Comments
Post a Comment