Click here to Skip to main content
Click here to Skip to main content

XSLTs to Assembly using XSLTC and ILMerge

, 12 Jun 2008
Rate this:
Please Sign up or sign in to vote.
This article is used to convert XSLT files into .NET assembly using XSLTC.exe and ILMerge.exe utility

Introduction

This article describes how to convert XSLT files into a single .NET assembly using XSLT.exe and ILMerge.exe utility.

I have developed an application (CompileXSLT.exe) which acts as a wrapper class to XSLTC and ILMerge which converts all the XSLT files into DLLs and merges all the scripts DLLs into a single Assembly which acts as a single XSLT resource to application code.

A test application loads the generated assembly and transforms couple of HTML files using the generated XSLT assembly file.

This will increase the performance and also hide the secured XSLT content from the public.

Background

Our project contains more than 500 XSLT files and a bunch of XML files. As a part of Framework 2.0 updates, we have used a new Framework 2.0 class for XSLT transform, XslCompiledTransfrom instead of Xsltransfrom. We identified some poor performance with the XSLT implemented pages. This article explains how to improve the performance of XSLT loading and transformation.

XSLT Utility

It’s a command line utility that can be used to compile multiple stylesheets into one assembly. It’s shipped with .NET Framework 2.0 Service Pack 1 or Windows SDK 6.0.

    xsltc [options] [/class:<name>] <sourceFile> [[/class:<name>] <sourceFile>...]
                          XSLT Compiler Options

                        - OUTPUT FILES -
        /out:<file>             Specify name of binary output file 
                                (default: name of the first file)
        /platform:<string>      Limit which platforms this code can run on: x86,
                                Itanium, x64, or anycpu, which is the default
                        - CODE GENERATION -
        /class:<name>           Specify name of the class for compiled stylesheet (short
                                form: /c)
        /debug[+|-]             Emit debugging information
        /settings:<list>        Specify security settings in the format
                                (dtd|document|script)[+|-],...
                                Dtd enables DTDs in stylesheets, document enables
                                document() function, script enables <msxsl:script>
                                element
                        - MISCELLANEOUS -
        @<file>                 Insert command-line settings from a text file
        /help                   Display this usage message (short form: /?)
        /nologo                 Suppress compiler copyright message

XSLT Compiler (xsltc.exe) link contains a detailed description.

ILMerge Utility

ILMerge is a utility that can be used to merge multiple assemblies into a single assembly. It is freely available for use from the Tools & Utilities page at the Microsoft .NET Framework Developer Center.

    ilmerge [/lib:directory]* [/log[:filename]] [/keyfile:filename [/delaysign]]
        [/internalize[:filename]] [/t[arget]:(library|exe|winexe)]
            [/closed] [/ndebug] [/ver:version] [/copyattrs [/allowMultiple]] [/xmldocs]
                [/attr:filename] [/targetplatform:<version />[,] | /v1 | /v1.1 | /v2]
            [/useFullPublicKeyForReferences] [/wildcards] [/zeroPeKind] [/allowDup:type]*
                [/union] [/align:n] /out:filename <primary /> [<other />...]

ILMerge link contains detailed description.

Compile XSLT

It’s a simple tool which is used to convert all the XSLT into assemblies and merge all the assemblies into a single assembly and a cleanup module is used to clean up all the unused generated assemblies.

CompileXSLT.exe <folder_Name> <Assembly_Name>

For example, if the XSLT files are placed in c:\project\XSLT\ folder, CompileXSLT.exe and XSLTC.exe, ILMerge.exe utilities should be placed in the same folder location. The below command line is used to generate the assembly MyXSLT.dll:

CompileXSLT.exe c:\project\xslt MyXSLT.dll

This tool generates MyXSLT.dll in c:\project\xslt folder location and makes sure both XSLTC.exe and ILMerge.exe utilities are placed in the same location.

It creates two batch files XSLT.bat and DLL.bat for XSLTC and ILMerge utility execution. XSLT batch file contains a list of XSLT file names with output DLL file name. The attached example contains two XSLT files MyTest1.xslt and MyTest2.xslt.

xsltc.exe /settings:script+ MyTest1.xslt MyTest2.xslt /out:MyXSLT.dll

The XSLTC utility generates one MyXSLT.dll and two scripts DLLs. The XSLTC.exe generates a script DLL if the XSLT file contains any scripts and the attached example contains scripts.

The second batch file DLL.bat contains a list of generated DLLs with output DLL file name.

ILMerge.exe /out:MyXSLT.dll MyXSLT.dll MyXSLT.script1.dll MyXSLT.script2.dll

GenerateXSLTBatch (CompileXSLT.exe)

This is a console application which generates a single XSLT assembly file as output. This application contains three main modules:

  1. Compile XSLT Files: Create a batch file and execute the batch file. The output contains MyXSLT.dll with a list of Scripts DLL.
  2. Merge DLL: Create a batch file and execute the batch file. The final assembly MyXSLT.dll contains all the scripts assemblies.
  3. DLL cleanup: This module cleans up all the unused generated DLLs.

The below method is used to execute the batch file:

System.Diagnostics.ProcessStartInfo pInfo = new System.Diagnostics.ProcessStartInfo();
pInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pInfo.WorkingDirectory = workFolder;
pInfo.FileName = fileToExec;
pInfo.Arguments = commandLineParams;
pInfo.CreateNoWindow = true;
pInfo.UseShellExecute = true;
System.Diagnostics.Process childProc = System.Diagnostics.Process.Start(pInfo);
childProc.WaitForExit();
int retCode = childProc.ExitCode;

LoadXSLTDLL

This is also a test console application used to load the generated XSLT assembly and generate a couple of HTML files based on the XSLT files.

The code snippet shown below is used to load and transform XSLT into HTML files:

string dllPath = AppDomain.CurrentDomain.BaseDirectory + "MyXSLT.dll";
XslCompiledTransform xslt = new XslCompiledTransform();

Assembly asmXslt = System.Reflection.Assembly.LoadFrom(dllPath);
Type xsltType = GetXSLTType(asmXslt, "MyTest1");
// Load MyTest1
xslt.Load(xsltType);
// Execute the transformation and output the results to a file.
xslt.Transform("books.xml", "20Per_discount_books.html");
// Load MyTest2
xsltType = GetXSLTType(asmXslt, "MyTest2");
xslt.Load(xsltType);
xslt.Transform("books.xml", "10Per_discount_books.html");

We can also include the below snippet to couple with the existing MSBuild scripts.

   <Exec Command ="ComipleXSLT &quot;$(WorkingDirectory)\Deploy\XSLTFiles&quot; 
        &quot;MyXSLT.dll&quot;"/>

Results

The tool will replace the physical XSLT files with the single assembly, which improves the performance of XSLT based pages by 10-15% and provides security to your XSLT files.

Reference

History

  • 12th June, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Saravana Prakash P
Web Developer
United States United States
I am a MCAD .NET web professional and working for Indian based MNC Company.
http://saravanaprakashp.blogspot.com
-Trust in Technology

Comments and Discussions

 
GeneralXsltc already does this Pinmembermikedm231-May-09 1:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 12 Jun 2008
Article Copyright 2008 by Saravana Prakash P
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid