From: Steve B. <Ste...@zv...> - 2001-08-14 01:19:24
|
TclXSLT v0.1alpha has been checked into the TclXML CVS repository on SourceForge. It has not been released as a package yet. The TEA setup is not complete (volunteers needed!). TclXSLT is a Tcl wrapper for the Gnome libxslt library. It has been tested with libxslt v1.0.1/libxml 2.4.1. These must be installed separately. This Tcl package allows a Tcl application to perform XSL transformations upon an XML document using an XSL stylesheet. Both of these XML documents are supplied as strings (ie. in memory). However, there is an option to supply the XML source document as a filename (this is a workaround to allow external entities to be resolved properly). An advantage of using this package is that the XSLT stylesheet is cached after it has been compiled. This makes subsequent uses of the stylesheet faster (the Tcl object mechanism is used). Also, XSLT extensions can be implemented using Tcl, see below. Example ------- package require xslt set ch [open stylesheet.xsl] set xsl [read $ch] close $ch set resultA [::xslt::transform $xsl -filename sourceA.xml] set resultB [::xslt::transform $xsl -filename sourceB.xml] --- The first use of the transform command compiled the stylesheet, the second use reused the compiled stylesheet. My testing shows that this can significantly improve the performance of large stylesheets, such as DocBook's. This is apart from the fact that libxslt is extremely fast anyway! Extensions ---------- A new feature of this package is that XSLT extensions can be implemented using Tcl. At the moment this is limited to the implementation of extension functions, passing strings as arguments and returning a string value. Future work will extend this to extension elements and allow handling of nodes and nodesets. This general-purpose mechanism allows extensions to be easily created, and registered/deregistered on-the-fly. To implement an extension, the Tcl application associates an XML Namespace with a Tcl Namespace. This is done using the '::xslt::extension add' command. All Tcl commands in the Tcl namespace are registered as extension functions. When an extension functions is used, its arguments are converted to strings and a Tcl script is constructed by appending the Tcl namespace, local name of the function and the arguments. The script is evaluated and the result is returned as a string. Example ------- *** XSL stylesheet (ext-test.xsl): <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:ext='http://tclxml.sf.net/XSLT/Test' extension-element-prefixes='ext'> <xsl:output method='text'/> <xsl:template match='/'> <xsl:text>Test value: "</xsl:text> <xsl:value-of select="ext:test('argument 1', 'argument 2')"/> <xsl:text>"</xsl:text> </xsl:template> </xsl:stylesheet> *** Tcl script: package require xslt namespace eval extTest { namespace export test } proc extTest::test args { return "extTest::test passed [llength $args] arguments" } set ch [open ext-test.xsl] set xsl [read $ch] close $ch ::xslt::extension add http://tclxml.sf.net/XSLT/Test ::extTest ::xslt::transform $xsl <Test/> Please discuss this package on the TclXML developers mailing list, tcl...@li..., or contact me directly. Enjoy, Steve Ball -- Steve Ball | XSLT Standard Library | Training & Seminars Zveno Pty Ltd | Web Tcl Complete | XML XSL Schemas http://www.zveno.com/ | TclXML TclDOM | Tcl, Web Development Ste...@zv... +---------------------------+--------------------- Ph. +61 2 6242 4099 | Mobile (0413) 594 462 | Fax +61 2 6242 4099 |