XSLT transformations with .Net
How the transformation works with .Net.
Now, as we create the result file we want the transformed output to populate, we apply the source XML,
arguments (params) and XML writer to the XSLT engines Transform() function. This is when/where the
transforation is performed and the end of any C#/.Net involvement for now.
The next entry will create some basic building blocks
Using .Net and C# you can do XSL transformations using XslCompiledTransform(). As input to the XSLT engine we use an XSLT sheet, settings (optional), parameters (optional), an XML source and the output path.
Note: Please note that if you need to use XSLT 2.0 or XPath 2.0 you should use a third party XSLT engine.
1: // C# code - using .Net 4.0
2:
3: // We want to enable document() functions and script() inside the XSLT code.
In order to allow this in using .Net we include this in the settings.
This gives us a bit more power as far as .Net XSLT support goes.
4: var settings = new XsltSettings(true, true);
5:
6: // This is the XSLT engine object
7: var xslTransform = new XslCompiledTransform();
8:
9: // We load the XSLT and settings to the engine.
10: xslTransform.Load(XSLTSheetPath, settings, new XmlUrlResolver());
11:
12: // As there is no way of generating e.g. date objects inside XSLT 1.0
(the only one supported by .Net) we do this outside the code and include
it as a parameter.
13: // Argument list (Params)
14: var xslArg = new XsltArgumentList();
15: // Grabbing current DateTime
16: DateTime time = DateTime.Now;
17: // Create a parameter which represents the current date and time.
18: xslArg.AddParam("date", "", time.ToString("s"));
Now, as we create the result file we want the transformed output to populate, we apply the source XML,
arguments (params) and XML writer to the XSLT engines Transform() function. This is when/where the
transforation is performed and the end of any C#/.Net involvement for now.
1: // Transform the file.
2: using (XmlWriter w = XmlWriter.Create(XMLResultPath))
3: {
4: xslTransform.Transform(XMLSourcePath, xslArg, w);
5: }
6:
7: // The entire transforming class could look like this:
8: using System;
9: using System.Diagnostics;
10: using System.Globalization;
11: using System.Xml;
12: using System.Xml.Xsl;
13:
14: namespace XSLTGenerator
15: {
16: class XMLTransformer
17: {
18: public void TransformXmlUsingXSLT(string XMLSourcePath,
string XSLTSheetPath, string XSDSchemaPath, string XMLResultPath)
19: {
20: try
21: {
22: // Creating settings to enable document() and script()
23: var settings = new XsltSettings(true, true);
24:
25: var xslTransform = new XslCompiledTransform();
26:
27: // Load XSLT stylesheet
28: xslTransform.Load(XSLTSheetPath, settings, new XmlUrlResolver());
29:
30: // Create the XsltArgumentList.
31: var xslArg = new XsltArgumentList();
32:
33: DateTime time = DateTime.Now;
34:
35: // Create a parameter which represents the current date and time.
36: xslArg.AddParam("date", "", time.ToString("s"));
37: // Create a parameter for GUID
38: xslArg.AddParam("id", "", new Guid().ToString());
39:
40: // Transform the file.
41: using (XmlWriter w = XmlWriter.Create(XMLResultPath))
42: {
43: xslTransform.Transform(XMLSourcePath, xslArg, w);
44: }
45: }
46: catch (Exception e)
47: {
48: Debug.WriteLine(e.Message);
49: }
50: }
51: }
52: }
The next entry will create some basic building blocks
Comments
Post a Comment