[lxml-dev] XSLT extension elements landed on trunk
Stefan Behnel
stefan_ml at behnel.de
Sun Mar 2 10:08:29 CET 2008
Hi,
the current trunk now has support for Python implemented XSLT extension
elements. It's sort of a sandbox environment with read-only Elements, where
you can do basically anything based on the stylesheet and the input document,
and then append some result subtree to the XSLT output tree.
Here's a short XSLT snippet that uses an extension, and a Python class that
provides such an extension:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myns="testns"
extension-element-prefixes="myns">
<xsl:template match="a">
<A>
<myns:myext><x>X</x><y>Y</y><z/></myns:myext>
</A>
</xsl:template>
</xsl:stylesheet>
class MyExt(etree.XSLTExtension):
def execute(self, context, self_node, input_node, output_parent):
# apply templates to my own children and process the result
for child in self_node:
for result in self.apply_templates(context, child):
if isinstance(result, basestring):
el = etree.Element("T")
el.text = result
else:
el = result
output_parent.append(el)
I don't remember when I first started thinking about this, but it was actually
pretty hard to get right until now. I uploaded some docs to the dev site:
http://codespeak.net/lxml/dev/xpathxslt.html#extension-elements
Note that this is currently an experimental feature that will go into lxml
2.1. Any comments and bug reports will be very much appreciated.
Have fun,
Stefan
More information about the lxml-dev
mailing list