Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python XSLT Transformation
from StringIO import StringIO
from lxml import etree
xslt_root = etree.XML('''\
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<foo><xsl:value-of select="/a/b/text()" /></foo>
</xsl:template>
</xsl:stylesheet>''')
transform = etree.XSLT(xslt_root)
f = StringIO('<a><b>Hello World!</b></a>')
doc = etree.parse(f)
result_tree = transform(doc)
root = etree.XML('<a><b>Text</b></a>')
result = transform(root)
xml = str(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment