Re: Replicating DTD information using XMLFilterBase and XMLGenerator

Stefan Behnel <[email protected]> Tue, 29 Jul 2008 07:42:03 +0200
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
Hi,

James Sulak wrote:
> Thanks, Stefan, for pointing me to lxml.  It looks like a good
> alternative to SAX in this situation.  However, I'm a little confused
> as to the best way to remove elements from the tree while keeping
> their tail text.  This is what I have so far:
> 
> context = etree.iterparse("test.xml")
> 
> for event, element in context:
>     for title in element.xpath("child::title"):

it's likely faster to use

    for title in element.iterchildren("title"):

here.

>         element.remove(title)
> 
> Do I need to explicitly assign the tail text to either the parent or
> the preceding sibling?

Yes, the tail text is part of the Element object. Take a look at the
"drop_tree" and "drop_tag" methods in lxml.html.

http://codespeak.net/svn/lxml/trunk/src/lxml/html/__init__.py

Stefan
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig