Get the xml-stylesheet processing instruction with lxml

I’m posting this recipe here because I had a hard time finding the information (eventually found in an lxml-dev thread).

So, you have an XML document which you’ve parsed with lxml.etree.  The doc has an xml-stylesheet processing instruction, and you want to apply the stylesheet to the doc.

>>> from lxml import etree
>>> doc = etree.parse('C:/Temp/report.xml')
>>> docroot = doc.getroot()
>>> pi = docroot.getprevious()
>>> if isinstance(pi, etree._XSLTProcessingInstruction):
	xslt = pi.parseXSL()
>>> transform = etree.XSLT(xslt)
>>> result_tree = transform(doc)

These commands from the Python interactive interpreter just demonstrate the necessary steps.  Obviously, there are other considerations to factor into actual code, such as security and exception handling.  Also, we are assuming here that the XSLT PI, if present, occurs immediately before the root node of the document.

Advertisements

,