I've attached a simple task which will take a XML file and apply a XSL
stylesheet/transformation to it and persist the results to disk e.g. <xslt
xml="MyData.xml" xsl="MyStylesheet.xsl" output="Output.html" />

--Neil

// NAnt - A .NET build tool
// Copyright (C) 2001-2002 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Neil Cowburn ([EMAIL PROTECTED])

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace NAnt.Contrib.Tasks 
{
        /// <summary>
        /// Performs XSL Transformations of XML data and outputs to a file on disk.
        /// </summary>
        /// <example>
        ///   <para>
        ///   Apply a XSL stylesheet to an XML data island and save the resulting HTML.
        ///   </para>
        ///   <code>
        ///     <![CDATA[
        /// <xslt xml="MyData.xml" xsl="MyStylesheet.xsl" output="Output.html" />
        ///     ]]>
        ///   </code>
        /// </example>
        [TaskName("xslt")]
        public class XslTransformTask : Task 
        {
                private FileInfo _xmlFile;
                private FileInfo _xslFile;
                private FileInfo _outputFile;

                /// <summary>
                /// The XML file containing the data to be transformed.
                /// </summary>
                [TaskAttribute("xml", Required=true)]
                [StringValidator(AllowEmpty=false)]
                public FileInfo XmlFile 
                {
                        get { return _xmlFile; } 
                        set { _xmlFile = value; }
                }

                /// <summary>
                /// The XSL stylesheet or transformation to be applied to <see 
cref="XmlFile"/>
                /// </summary>
                [TaskAttribute("xsl", Required=true)]
                [StringValidator(AllowEmpty=false)]
                public FileInfo XslFile 
                {
                        get { return _xslFile; } 
                        set { _xslFile = value; }
                }

                /// <summary>
                /// The file that the result of the transformed will be persisted to.
                /// </summary>
                [TaskAttribute("output", Required=true)]
                [StringValidator(AllowEmpty=false)]
                public FileInfo OutputFile 
                {
                        get { return _outputFile; } 
                        set { _outputFile = value; }
                }

                protected override void ExecuteTask()
                {
                        //Create a new XslTransform object.
                        XslTransform xslt = new XslTransform();

                        //Load the stylesheet.
                        xslt.Load(_xslFile.FullName);

                        //Create a new XPathDocument and load the XML data to be 
transformed.
                        XPathDocument mydata = new XPathDocument(_xmlFile.FullName);

                        //Create an XmlTextWriter which will output the result of the 
transformation.
                        XmlWriter writer = new XmlTextWriter(_outputFile.FullName, new 
System.Text.ASCIIEncoding());

                        //Transform the data and write the output.
                        xslt.Transform(mydata,null,writer, null);
                }
        }
}

Reply via email to