Skip to content

Instantly share code, notes, and snippets.

@roorz
Created July 24, 2012 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roorz/3171696 to your computer and use it in GitHub Desktop.
Save roorz/3171696 to your computer and use it in GitHub Desktop.
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