"""Demonstrates the various ways of passing input XML and XSL to Pyana"""
import Pyana
inputExampleXSL = r'''
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="message"><xsl:value-of select="child::text()"/></xsl:template>
</xsl:stylesheet>
'''
inputExampleXML = r'''
<message>Hello World!</message>
'''
# From strings
print Pyana.transform2String(source=inputExampleXML, style=inputExampleXSL)
# From URIs
print Pyana.transform2String( source=Pyana.URI('http://pyana.sourceforge.net/examples/helloworld.xml'),
style=Pyana.URI('http://pyana.sourceforge.net/examples/helloworld.xsl'))
# Mix and match
print Pyana.transform2String( source=inputExampleXML,
style=Pyana.URI('http://pyana.sourceforge.net/examples/helloworld.xsl'))
# From Reader object
# This example is a bit contrived but...
class ReadTester:
"""This class lowercases the input buffer"""
def __init__(self, buffer):
self.buffer = buffer
def read(self, readSize = None):
if readSize is None:
readSize = len(self.buffer)
readBuffer = self.buffer[:readSize]
self.buffer = self.buffer[readSize:]
return readBuffer.lower()
print Pyana.transform2String( ReadTester(inputExampleXML),
inputExampleXSL) # Can't lowercase the XSL, it would ruin the namespace
|