EcStart PHP 技術討論論壇's Archiver

FIEND 發表於 2008-4-28 16:56

XSLT tools for Python

<div><a href="http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/python-xslt">XSLT tools for Python</a></div>
        
        
         
            <p xmlns:a="http://uche.ogbuji.net/tech/projects/akara/">This item used to discuss <a href="http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/basic-xslt">how to use XSLT in 4Suite</a>.  It is now generalized to cover XSLT processing in general for Python.  for the former item, <a href="http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/basic-xslt">click here</a> </p>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://4suite.org/">4Suite</a></li></ul>
            <div xmlns:a="http://uche.ogbuji.net/tech/projects/akara/" class="programlisting">
              <pre>from Ft.Xml.Xslt import Transform, TransformPath<br><br>result = TransformPath(xmlfile, xsltfile)<br></pre>
            </div>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://xmlsoft.org/XSLT/python.html">libxml/Python</a>, a wrapper for the libxml</li></ul>
            <div xmlns:a="http://uche.ogbuji.net/tech/projects/akara/" class="programlisting">
              <pre>import libxml2<br>import libxslt<br><br>styledoc = libxml2.parseFile("test.xsl")<br>style = libxslt.parseStylesheetDoc(styledoc)<br>doc = libxml2.parseFile("test.xml")<br>result = style.applyStylesheet(doc, None)<br>style.saveResultToFilename("foo", result, 0)<br>style.freeStylesheet()<br>doc.freeDoc()<br>result.freeDoc()<br></pre>
            </div>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://www.rexx.com/%7Edkuhlman/libxsltmod.html">libxsltmod</a>, alternate libxslt/Python binding</li></ul>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://maki.sourceforge.net/manual/index.html">maki</a>, a simple, flexible framework that allows you to use XML, XSLT, your webserver, and Python to serve web pages.</li></ul>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://pirxx.sourceforge.net/">PIRXX</a> provides a Python InteRface to Xerces and Xalan, integrating it into PyXML.</li></ul>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://pyana.sourceforge.net/">Pyana</a>, a wrapper for the Xalan XSLT processor in Python:</li></ul>
            <div xmlns:a="http://uche.ogbuji.net/tech/projects/akara/" class="programlisting">
              <pre>import Pyana<br>s = Pyana.transformToFile(<br>        Pyana.URI(file), <br>        Pyana.URI(xsl_file),<br>        output)<br></pre>
            </div>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://www.jython.org/">Jython</a> and JAXP, thanks to <a href="http://weblog.hotales.org/view/python/2004/09/01/0">Jarno Virtanen</a>:</li></ul>
            <div xmlns:a="http://uche.ogbuji.net/tech/projects/akara/" class="programlisting">
              <pre>from java.io import FileInputStream<br>from java.io import ByteArrayOutputStream<br><br>from javax.xml.transform import TransformerFactory<br>from javax.xml.transform.stream import StreamSource<br>from javax.xml.transform.stream import StreamResult<br><br>input = FileInputStream("test.xsl")<br>xslSource = StreamSource(input)<br>xslTemplate = TransformerFactory.newInstance().newTemplates(xslSource);<br>transformer = xslTemplate.newTransformer()<br><br>output = ByteArrayOutputStream()<br>result = StreamResult(output)<br><br>source = StreamSource(FileInputStream("source.xml"))<br>transformer.transform(source, result)<br><br>print output # .. or output.toString()<br></pre>
            </div>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li>MSXML 4.0, thanks to <a href="http://mail.python.org/pipermail/python-list/2006-September/363891.html">Ross Ridge</a>:</li></ul>
            <div xmlns:a="http://uche.ogbuji.net/tech/projects/akara/" class="programlisting">
              <pre>from os import environ<br>import win32com.client<br><br>def buildPage():<br>        xsluri = 'xsl/plainpage.xsl'<br>        xmluri = 'website.xml'<br><br>        xsl = win32com.client.Dispatch("Msxml2.FreeThreadedDOMDocument.4.0")<br>        xml = win32com.client.Dispatch("Msxml2.DOMDocument.4.0")<br>        xsl.load(xsluri)<br>        xml.load(xmluri)<br><br>        xslt = win32com.client.Dispatch("Msxml2.XSLTemplate.4.0")<br>        xslt.stylesheet = xsl<br>        proc = xslt.createProcessor()<br>        proc.input = xml<br><br>        params = {"url":environ['QUERY_STRING'].split("=")[1]}<br>        for i, v in enumerate(environ['QUERY_STRING'].split("/")[1:]):<br>            params["selected_section%s" % (i + 1)] = "/" + v<br><br>        for param, value in params.items():<br>                proc.addParameter(param, value)<br>        proc.transform()<br>        return proc.output<br><br>print "Content-Type: text/html\n\n"<br>print buildPage()<br></pre>
            </div>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://www.gingerall.org/charlie/ga/xml/x_sabpyth.xml">Python extension to Sablotron</a></li></ul>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://zopexmlmethods.sourceforge.net/">Zope XML Methods</a> provides methods to apply to Zope objects for XML/XSLT processing, supporting 4Suite, libxslt, Sablotron and Pyana</li></ul>
            <p xmlns:a="http://uche.ogbuji.net/tech/projects/akara/">The following threads include some discussion of XSLT processing options in Python: </p>
            <ul xmlns:a="http://uche.ogbuji.net/tech/projects/akara/"><li><a href="http://mail.python.org/pipermail/xml-sig/2003-June/009555.html">using xslt with python</a></li></ul>

FIEND 發表於 2008-4-28 18:16

[url]http://codespeak.net/lxml/[/url]

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.