[lxml-dev] Adding a Stylesheet PI when creating new XML documents
Stefan Behnel
stefan_ml at behnel.de
Mon Mar 26 22:59:32 CEST 2007
Hi Lee,
Lee Brown wrote:
> As a work-around, I tried creating my XML document from a prototype
> string that contained a stylesheet PI and an empty root element. That
> part worked O.K. (tree.getroot().getprevious() returned the PI) but then
> the PI was promptly discarded upon reserialization by etree.tostring()!!!
Right, I noticed this also. It's not as simple as "here's the fix", though.
The right way to deal with this would be to use the "xmlsave" API in libxml2.
However, this API had a couple of problems with serialisation in the past, so
this would require us to depend on a rather recent version of libxml2 (I think
it was at least 2.6.24). I'm actually quite happy that we currently have lxml
working across the last ten versions of libxml2 without major problems. You
can see the current behaviour as a compromise between the targeted behaviour
of Fredrik's ElementTree (which, for example, avoids XML declarations wherever
possible) and the backwards compatibility required to work around libxml2's
bugs. We could drop either of the two, i.e., serialise different (though
equivalent) XML or start requiring libxml2 2.6.24. Given these two
possibilities, I'd personally vote for keeping it the way it is.
Still, I admit that this is annoying. While you can serialise to a StringIO
object by using ET.write() - which should not suffer from this problem - it
would definitely be better to provide the same behaviour for string
serialisation, too.
Maybe I could try to fix it at least for more recent libxml2 versions - if I
find the time.
Stefan
More information about the lxml-dev
mailing list