root = etree.Element('root')
a = etree.SubElement(root, 'a')
b = etree.SubElement(root, 'b')
root.addprevious(etree.PI('xml-stylesheet', 'type="text/xsl" href="my.xsl"'))
print(etree.tostring(root, pretty_print=True, xml_declaration=True,
    encoding='utf-8'))
Creating a doctype with lxml's etree