|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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. BackgroundOur 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, XSLT UtilityIt’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 UtilityILMerge 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:
ILMerge link contains detailed description. Compile XSLTIt’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:
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;
LoadXSLTDLLThis 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 "$(WorkingDirectory)\Deploy\XSLTFiles"
"MyXSLT.dll""/>
ResultsThe 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. ReferenceHistory
|
||||||||||||||||||||||