Re: [xslt] "re-use" LibXSLT compiled stylesheet collection

Johann Richard wrote:
> I was browsing on the LiBXSLT site but couldn't find a satisfactory answer to this question:
> Is it -- somehow -- possible to "re-use" a stylesheet that has been read, parsed and compiled by LiBXSLT? Ideally, for large collections of stylesheets (like DocBook) that do not change that often, it would be desirable to skip the step of compilation, i.e. write out the compiled collection in a persistent way to disk or so, and read it back later when you want to re-use the collection. From what I read about how variables and parameters are handled internally, this should be possible w/ LibXSLT.

Yes it is, by using a framework that binds libxslt.
One such framework is TclXSLT (see,
and there are others available for Perl and Python (at least).
TclXSLT allows you to script the handling of DOM documents and
stylesheets, including caching of the in-memory structures [*].

However, such caching only lasts as long as the process.
As has been pointed out by others, there is no way to
save the compiled stylesheet for a later run.

Caching result documents on disk can be done, and often has
significant advantages.

Using caching techniques is often best applied in a Web server,
ala Apache/mod_tcl.

> I suppose that the gain from this would be considerable? Is there already a way to do it or anybody have written such code and can give some hints?

The gain achieved depends on the size/complexity of the stylesheet
being used as well as the number of documents being processed,
as the cost of parsing and compiling the stylesheet is
amortised against the number of documents being transformed.

[*] a little snippet to demonstrate:

package require xslt

# First load the stylesheet
set ch [open docbook/html/docbook.xsl]
set doc [dom::parse [read $ch] -baseuri 
close $ch
set ssheet [xslt::compile $doc]

# Now transform a bunch of documents
foreach fname [glob *.xml] {
     # Open the next document
     set ch [open $fname]
     set doc [dom::parse [read $ch]]
     close $ch

     # Do the transform using the cached stylesheet
     set result [$ssheet transform $doc]

     # Write the result to a new file
     set ch [open $fname.result w]
     puts [dom::serialize $result]
     close $ch

     # Clean-up the in-memory structures
     dom::destroy $doc
     dom::destroy $result

Steve Ball            |   XSLT Standard Library   | Training & Seminars
Zveno Pty Ltd         |     Web Tcl Complete      |   XML XSL Schemas |      TclXML TclDOM        | Tcl, Web Development  +---------------------------+---------------------
Ph. +61 2 6242 4099   |   Mobile (0413) 594 462   | Fax +61 2 6242 4099

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]