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.
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.
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
/debug[+|-] Emit debugging information
/settings:<list> Specify security settings in the format
Dtd enables DTDs in stylesheets, document enables
document() function, script enables <msxsl:script>
- 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 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]]
[/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.
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
This is a console application which generates a single XSLT assembly file as output. This application contains three main modules:
- Compile XSLT Files: Create a batch file and execute the batch file. The output contains MyXSLT.dll with a list of Scripts DLL.
- Merge DLL: Create a batch file and execute the batch file. The final assembly MyXSLT.dll contains all the scripts assemblies.
- 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);
int retCode = childProc.ExitCode;
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");
xsltType = GetXSLTType(asmXslt, "MyTest2");
We can also include the below snippet to couple with the existing MSBuild scripts.
<Exec Command ="ComipleXSLT "$(WorkingDirectory)\Deploy\XSLTFiles"
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.
- 12th June, 2008: Initial post