Well I'm running version 2.0.5 and I guess it was a pebcak issue since the .addprevious() worked for me as well.

I swear I looked over the documentation, but just missed the obvious..

Thanks
Bob

On Wed, Oct 28, 2009 at 2:05 PM, Chuck Bearden <cfbearden@gmail.com> wrote:
On Wed, Oct 28, 2009 at 5:53 AM, Bob Brandt <bob@brandt.ie> wrote:
> First let me say I love lxml!
>
> I have noticed one problem though... (Although I am unsure whether the
> problem is with lxml or PEBCAK)
>
> I need to insert comments both before and after the root element.  For what
> I can see this is not only perfectly legal but specifically allowed in the
> RFCs (although for the life of me I can find the relevant sections again)
>
> I can not figure out how to perform this.  And from my testing it appears
> that even if I import (either tostring or parse) ignores a document which
> has comments before and after, it just ignores them...
>
> For now I am running a "workaround" with just outputs the comments, then the
> xml, then the comments, but it would be much better if lxml could do it
> since there could be problems with the XML Declaration.

The .addprevious() and .addnext() methods called on the root element
seem to work for me:

>>> from lxml import etree
>>> from StringIO import StringIO
>>> etree.__version__
u'2.1.5'
>>> xmlString = '''<?xml version="1.0"?>
... <root>
...   <branch>
...     <leaf/>
...   </branch>
... </root>'''
>>> xmlTree = etree.parse(StringIO(xmlString))
>>> xmlTree.getroot().addprevious(etree.PI('prior_PI'))
>>> xmlTree.getroot().addnext(etree.Comment('trailing comment'))
>>> print etree.tostring(xmlTree, xml_declaration=True)
<?xml version='1.0' encoding='ASCII'?>
<?prior_PI ?><root>
 <branch>
   <leaf/>
 </branch>
</root><!--trailing comment-->
>>>

This also works for me with lxml 2.2.2.  What version are you using?

Likewise, my installation doesn't ignore the comments and PIs when I
serialize and reparse the XML:

>>> xmlString2 = etree.tostring(xmlTree, xml_declaration=True)
>>> xmlTree2 = etree.parse(StringIO(xmlString2))
>>> print etree.tostring(xmlTree2, xml_declaration=True)
<?xml version='1.0' encoding='ASCII'?>
<?prior_PI ?><root>
 <branch>
   <leaf/>
 </branch>
</root><!--trailing comment-->
>>>

Chuck



--
The problem with socialism is that you eventually run out of other people's money.  -  Margaret Thatcher