Project

Profile

Help

how to pre-compile style sheets?

Added by Anonymous over 15 years ago

Legacy ID: #5642760 Legacy Poster: massi (massimassi)

hi gurus, I'm using saxonB in a .NET application. I'd like to know if it is possible to pre-compile my style sheets and then serialize them on file, to use them in a faster way. I noticed that the transform.exe utility that comes with saxonb allows to use compiled style sheets, but how to build these files? I tried to serialize the XsltTransformer object with a BinaryFormatter, but unfortunately the Controller object - contained in XsltTransformer - is not Serializable. Otherwise, any suggestion to speed up compilation? I simply use Compile(). Thank you :-) massi


Replies (8)

Please register to reply

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5642806 Legacy Poster: Michael Kay (mhkay)

There is a command java net.sf.saxon.Compile which will compile a stylesheet and save it to disk, from where it can be loaded using java net.sf.saxon.Transform Details are at http://www.saxonica.com/documentation/using-xsl/compiling.html To be honest, I wouldn't recommend using it. Loading the serialized precompiled stylesheet from disk takes almost as long as recompiling from scratch. If you can save the compiled stylesheet in memory, of course (specifically, the JAXP Templates object) that's a different matter - that is always well worth doing. >I tried to serialize the XsltTransformer object with a BinaryFormatter The XsltTransformer/Controller is not the object you want to reuse. It's the XsltExecutable, which is supported by an underlying PreparedStylesheet. Michael Kay http://www.saxonica.com/

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5643847 Legacy Poster: massi (massimassi)

hi, thanks for the fast and good answer. I'm not using the XsltExecutable, but the XsltTransformer in this way: XsltTransformer transformer; transformer = processor.NewXsltCompiler().Compile(uri).Load(); transformer.InitialContextNode = input; Serializer serializer = new Serializer(); MemoryStream memoryStream = new MemoryStream(); serializer.SetOutputStream(memoryStream); transformer.Run(serializer); I'll search XsltExecutable in the documentation. Anyway, I'll cache in memory. thanks a lot massi

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5643898 Legacy Poster: Michael Kay (mhkay)

transformer = processor.NewXsltCompiler().Compile(uri).Load(); The Compile step generates an XsltExecutable; the Load step converts this to an XsltTransformer. The key thing about the XsltExecutable is that it's thread-safe - once created, it is immutable and can be executed as many times as you like in series or in parallel.

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5645410 Legacy Poster: Phil V (bonekrusher)

Here is an example based on the samples provided: private void compileStylesheet(string xslt, TextBox textbox) { // Create a Processor instance. Processor processor = new Processor(); // Create the XSLT Compiler XsltCompiler compiler = processor.NewXsltCompiler(); // Create a list to hold the error information compiler.ErrorList = new ArrayList(); // Attempt to compile the stylesheet and display the errors try { compiler.Compile(new Uri(xslt)); console.Text += "Stylesheet compilation succeeded"; } catch (System.Exception textbox.Text) { ("\r\n" + ("Stylesheet compilation failed with " + (compiler.ErrorList.Count.ToString + (" errors" + "\r\n")))); StaticError myerror; foreach (myerror in compiler.ErrorList) { textbox.Text = (textbox.Text + ("At line " + (myerror.LineNumber.ToString + (": " + (myerror.Message.ToString + "\r\n"))))); } } }

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5661488 Legacy Poster: massi (massimassi)

thanks a lot guys, I'll try to serialize an XsltExecutable :-) massi

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5818149 Legacy Poster: massi (massimassi)

hi guys, I'm back to serialization. Unfortunately, when I try to Serialize an XsltExecutable object, I have an exception because PreparedStylesheet has not Serializable attribute. I think it could be useful to serialize the XsltExecutable for repetitive operations. Does turning PreparedStylesheet to Serializable involve big changes to software? Anyway, I compile my stylesheets only once at startup, as Michael told me. thank you bye

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5818256 Legacy Poster: Michael Kay (mhkay)

The class PreparedStylesheet is declared Serializable (I'm not sure why you would think otherwise), and it is serialized when you use the net.sf.saxon.Compile command from the command line. See http://www.saxonica.com/documentation/using-xsl/compiling.html for further details. Serialization (compiling) of stylesheets isn't supported via the s9api interface. It's not something I encourage particularly, because the performance gain achieved when loading from a serialized copy on disk, versus recompiling from the original source, really isn't very worthwhile.

RE: how to pre-compile style sheets? - Added by Anonymous over 15 years ago

Legacy ID: #5818437 Legacy Poster: massi (massimassi)

ok, I was trying to serialize in C# via s9api and I catched an exception: the class PreparedStylesheet is not declared Serializable. I'll simply recompile on startup. thanks a lot massimo

    (1-8/8)

    Please register to reply