The output of 4Suite's XSLT component is controlled by special writer classes. One of these, DomWriter, is designed to create a DOM node that represents the output. The following snippet illustrates using the DomWriter:
from Ft.Xml import InputSource from Ft.Xml.Domlette import Print from Ft.Xml.Xslt import Processor, DomWriter src = "<greeting>hello world</greeting>" sty = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" encoding="us-ascii"/> <xsl:template match="/"> <result> <xsl:copy-of select="."/> </result> </xsl:template> </xsl:stylesheet>""" src_isrc = InputSource.DefaultFactory.fromString(src, 'urn:dummy') sty_isrc = InputSource.DefaultFactory.fromString(sty, 'urn:sty') p = Processor.Processor() p.appendStylesheet(sty_isrc) wr = DomWriter.DomWriter() p.run(src_isrc, writer=wr) doc2 = wr.getResult() Print(doc2, encoding='us-ascii')
And here's another:
TRANSFORM = """<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="spam"> <xsl:call-template name="output-denormalized"> <monty>snake</monty> </xsl:template> <xsl:template match="@*"> <xsl:copy/> </xsl:template> <!-- Used to demonstrate that DOM writer normalizes output text nodes for you --> <xsl:template name="output-denormalized"> <spam> <xsl:value-of select="'eg'"/> <xsl:text>gs</xsl:text> </spam> </xsl:template> </xsl:stylesheet> """ SOURCE = """<spam xmlns:x='http://spam.com'>eggs<monty>python</monty></spam>""" from Ft.Xml.Xslt import Processor, DomWriter processor = Processor.Processor() from Ft.Xml import InputSource transform = InputSource.DefaultFactory.fromString(TRANSFORM, "http://spam.com/boo.xslt") processor.appendStylesheet(transform) source = InputSource.DefaultFactory.fromString(SOURCE, "http://spam.com/doc.xml") writer = DomWriter.DomWriter() #doc is the DOM node represernting the transform output processor.run(source, writer=writer) doc = writer.getResult() #Talk about a cheap DOM Iterator. Generators rule! from __future__ import generators def InOrderIterator(node): yield node for child in node.childNodes: for cn in InOrderIterator(child): yield cn return from xml.dom import Node for node in InOrderIterator(doc): if node.nodeType == Node.TEXT_NODE: print "TEXT: ", node.data if node.nodeType == Node.ELEMENT_NODE: print "ELEMENT: ", node.nodeName
