MSBuild XSLT Task

Regarding my former article now I’d like to give you a further example for a helpful MS Build task – namely XSLT.

The following task implementation takes the definitions of a source file, a target file as well as a XSLT file and transforms the file by using these properties. Internally it uses the method XslCompiledTransform.Transform() for the transformation process. As you can see at line 40-43 it’s a only a simple code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
 
namespace XSLTTask
{
    public class XSLT : Task
    {
        private string xsltFile;
 
        private string sourceXmlFile;
 
        private string targetXmlFile;
 
        [Required]
        public string XsltFile
        {
            get { return xsltFile; }
            set { xsltFile = value; }
        }
 
        [Required]
        public string SourceXmlFile
        {
            get { return sourceXmlFile; }
            set { sourceXmlFile = value; }
        }
 
        [Required]
        public string TargetXmlFile
        {
            get { return targetXmlFile; }
            set { targetXmlFile = value; }
        }
 
        public override bool Execute()
        {
            bool result = true;
 
            Log.LogMessage("Transforming from {0} to {1} using {2}",
                SourceXmlFile, TargetXmlFile, XsltFile);
 
            try
            {
                XslCompiledTransform xslTransform = GetXslTransform(XsltFile);
                XmlReader xmlReader = GetXmlReader(SourceXmlFile);
                XmlWriter xmlWriter = GetXmlWriter(TargetXmlFile);
                xslTransform.Transform(xmlReader, xmlWriter);
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e);
                result = false;
            }
 
            return result;
        }
 
        XslCompiledTransform GetXslTransform(String file)
        {
            XslCompiledTransform xslTransform = new XslCompiledTransform();
            xslTransform.Load(GetXmlReader(file));
            return xslTransform;
        }
 
        XmlReader GetXmlReader(String file)
        {
            return new XmlTextReader(GetTextReader(file));
        }
 
        XmlWriter GetXmlWriter(String file)
        {
            TextWriter textWriter = new StreamWriter(file);
            return new XmlTextWriter(textWriter);
        }
 
        TextReader GetTextReader(String file)
        {
            return new StreamReader(file);
        }
    }
}

And here comes the task definition embedded in a MSBuild project file:

1
2
3
4
5
6
7
8
9
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
  DefaultTargets="Default">
  <UsingTask AssemblyFile="XSLTTask.dll" TaskName="XSLT" />
  <Target Name="Default">
    <XSLT SourceXmlFile="input.xml"
          TargetXmlFile="output.xml"
          XsltlFile="transform.xslt"  />
  </Target>
</Project>