#!/usr/bin/env python

import sys
import StringIO

from lxml import etree

document = etree.parse(StringIO.StringIO('<element><subelement xml:id="foo"/></element>'))
root = document.getroot()
subelement = root[0]

new_subelement = etree.Element('subelement')
for name, value in subelement.attrib.items():
    new_subelement.set(name, value)

# Uncommenting this next line not only alters the output of the root document
# but shows us that the element is being created just fine.
# etree.dump(new_subelement)

# This next line could also be insert etc
root.append(new_subelement)

# Uncommenting this next line shows us that the element has been mangled.
# etree.dump(root[1])

document.write(sys.stdout)
sys.stdout.write('\n')
