"""Demonstrates the Transformer object.
"""
import Pyana
import sys
import time
xsl = r'''
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:param name="excitement"/>
<xsl:template match="message"><xsl:value-of
select="concat(child::text(), $excitement)"/></xsl:template>
</xsl:stylesheet>
'''
xml = '<message>Hello World</message>'
t = Pyana.Transformer()
# Python XPath extensions can be installed on a per-transformation object
# basis using:
# t.installExtension()
# t.installExtensionWithContext()
# t.removeExtension
t.setStylesheetParams({'excitement' : "'!'"})
# Disable validation for source documents (disabled is the default)
t.useValidation = 0
startTime = time.clock()
for i in range(100):
t.transform2String(xml, xsl)
print 'Time to transform (x100) without compilation: %.4fs' % (time.clock() - startTime)
compiled = t.compileStylesheet(xsl)
parsed = t.parseSource(xml)
startTime = time.clock()
for i in range(100):
t.transform2String(parsed, compiled)
print 'Time to transform (x100) with compilation: %.4fs' % (time.clock() - startTime)
|