You are here

xml

Dropping An Element In An Iterative Parse

Using lxml's etree to iteratively parse an XML document and I wanted to drop a specific element from the stream...

        for event, element in etree.iterparse(self.rfile, events=("end",)):
            if (event == 'end') and (element.tag == 'row'):
                self.wfile.write(etree.tostring(element))
            elif (event == 'end') and (element.tag == name_of_element_to_drop):
                element.getparent().remove(element) # drop element

The secret sauce is: element.getparent().remove(element)

Some Random xsltproc Tips

The xsltproc command allows the processing of an XML document with an XSLT template.

xsltproc rentalouts.xslt rentalouts.xml

Text 1: Perform the transform "rentalouts.xslt" on the document "rentalouts.xml".

A lesser known feature of xsltproc is the ability to pass parameters - these become XSLT variables - to the transformation.

XSLT Transform to TXT, with LXML

Maybe this should be obvious, but it wasn't to me. I've got an XML document and an XSLT stylesheet. But that stylesheet just produces text, not XML; it is essentially a template for an e-mail. So I was extending OIE's transformAction for performing XSLT transforms that produce other than XML... but the documentation is a bit thin and every example is XML results. The trick is pretty simple, just

unicode(result)

and make sure [of course] that you have

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer