issue: python xml append element inside a for loop
<p>thanks for taking the time with this one.
i have an xml file with an element called selectionset. the idea is to take that element and modify some of the subelements attributes and tails, that part i have done.
the shady thing for me to get is why when i try to add the new subelements to the original (called selectionsets) its only pushing the last on the list inplist</p>

<pre><code>    import xml.etree.ElementTree as etree
from xml.etree.ElementTree import *
from xml.etree.ElementTree import ElementTree
tree=ElementTree()
tree.parse('STRUCTURAL.xml')
root = tree.getroot()
col=tree.find('selectionsets/selectionset')
#find the value needed
val=tree.findtext('selectionsets/selectionset/findspec/conditions/condition/value/data')
setname=col.attrib['name']
listnames=val + " 6"
inplist=["D","E","F","G","H"]
entry=3
catcher=[]
ss=root.find('selectionsets')
outxml=ss
for i in range(len(inplist)):
    str(val)
    col.set('name',(setname +" "+ inplist[i]))
    col.find('findspec/conditions/condition/value/data').text=str(inplist[i]+val[1:3])
    #print (etree.tostring(col)) #everything working well til this point
    timper=col.find('selectionset')
    root[0].append(col)
    # new=etree.SubElement(outxml,timper)
#you need to create a tree with element tree before creating the xml file

itree=etree.ElementTree(outxml)
itree.write('Selection Sets.xml')
print (etree.tostring(outxml))

# print (Test_file.selectionset())


#Initial xml
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://download.autodesk.com/us/navisworks/schemas/nw-exchange-12.0.xsd" units="ft" filename="STRUCTURAL.nwc" filepath="C:\Users\Ricardo\Desktop\Comun\Taller 3"&gt;
  &lt;selectionsets&gt;
    &lt;selectionset name="Column Location" guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8"&gt;
      &lt;findspec mode="all" disjoint="0"&gt;
        &lt;conditions&gt;
          &lt;condition test="contains" flags="10"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;C-A &lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;/selectionsets&gt;
&lt;/exchange&gt;

#----Current Output
&lt;selectionsets&gt;
    &lt;selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"&gt;
      &lt;findspec disjoint="0" mode="all"&gt;
        &lt;conditions&gt;
          &lt;condition flags="10" test="contains"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;H-A&lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"&gt;
      &lt;findspec disjoint="0" mode="all"&gt;
        &lt;conditions&gt;
          &lt;condition flags="10" test="contains"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;H-A&lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"&gt;
      &lt;findspec disjoint="0" mode="all"&gt;
        &lt;conditions&gt;
          &lt;condition flags="10" test="contains"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;H-A&lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"&gt;
      &lt;findspec disjoint="0" mode="all"&gt;
        &lt;conditions&gt;
          &lt;condition flags="10" test="contains"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;H-A&lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"&gt;
      &lt;findspec disjoint="0" mode="all"&gt;
        &lt;conditions&gt;
          &lt;condition flags="10" test="contains"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;H-A&lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"&gt;
      &lt;findspec disjoint="0" mode="all"&gt;
        &lt;conditions&gt;
          &lt;condition flags="10" test="contains"&gt;
            &lt;category&gt;
              &lt;name internal="LcRevitData_Element"&gt;Element&lt;/name&gt;
            &lt;/category&gt;
            &lt;property&gt;
              &lt;name internal="lcldrevit_parameter_-1002563"&gt;Column Location Mark&lt;/name&gt;
            &lt;/property&gt;
            &lt;value&gt;
              &lt;data type="wstring"&gt;H-A&lt;/data&gt;
            &lt;/value&gt;
          &lt;/condition&gt;
        &lt;/conditions&gt;
        &lt;locator&gt;/&lt;/locator&gt;
      &lt;/findspec&gt;
    &lt;/selectionset&gt;
  &lt;/selectionsets&gt;
</code></pre>

<p>Here's what I've been able to put together and it looks like it'll do what you're looking for. Here are the main differences: (1) This will iterate over multiple selectionset items (if you end up with more than one), (2) It creates a deepcopy of the element before modifying the values (I think you were always modifying the original "col"), (3) It appends the new selectionset to the selectionsets tag rather than the root.</p>

<p>Here's the <a href="https://docs.python.org/3/library/copy.html" rel="nofollow">deepcopy documentation</a></p>

<pre><code>import xml.etree.ElementTree as etree
import copy

tree=etree.ElementTree()
tree.parse('test.xml')
root = tree.getroot()
inplist=["D","E","F","G","H"]

for selectionset in tree.findall('selectionsets/selectionset'):
    for i in inplist:
        col = copy.deepcopy(selectionset)
        col.set('name', '%s %s' % (col.attrib['name'], i))
        data = col.find('findspec/conditions/condition/value/data')
        data.text = '%s%s' % (i, data.text[1:3])
        root.find('selectionsets').append(col)

itree = etree.ElementTree(root)
itree.write('Selection Sets.xml')
</code></pre>

python xml