Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

XSL Editor Control

, 13 Jun 2009 CPOL
A basic WinForms XSL editor control
XSLEditor_demo.zip
Demo
bin
Release
Common.Library.dll
XSL.Library.dll
XSL.Library.RegularExpressions.dll
XSLEditor.exe
Lib
Common.Library.dll
XSL.Library.dll
XSL.Library.RegularExpressions.dll
Properties
Settings.settings
Resources
PackageBodyTemplate.xsl
XSLEditorDemo.csproj.user
XSLEditorDemo.suo
XSLEditor_src.zip
Source
Common.Library
bin
Release
Common.Library.dll
Common.Library.csproj.user
Controls
Properties
XSL.Library
bin
Release
Common.Library.dll
XSL.Library.dll
XSL.Library.RegularExpressions.dll
ClassDiagram.cd
mssccprj.scc
Properties
vssver2.scc
Resources
1.bmp
binoc.gif
copy.png
cut.png
debug.gif
find in doc.gif
find_arr.png
find_doc.gif
find_doc.png
intellisense.gif
JOB1.gif
JOB11.gif
KEY04.gif
networking.gif
new.png
NEW16.png
OBJECT.gif
open.gif
open.png
PAD_ILL.gif
paste.png
pen.png
Print Preview.gif
print.png
print2.png
refresh.png
run.gif
save.png
saveas2.png
Tasks.gif
tick.gif
vssver2.scc
wordwrap.gif
xsl.gif
xsl2.gif
xsl2.ico
Transform
vssver2.scc
XSL.Library.csproj.user
XSL.Library.csproj.vspscc
XSL.Library.RegularExpressions.dll
XSLEditorControlSource.suo
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Windows.Forms;

namespace XSL.Library
{   
    public static class TransformUtility
    {
        public static TransformResults Transform(FileInfo xmlFile, string xsl,
            TransformSettings settings)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile.FullName);
            return Transform(xmlDoc, new XPathDocument(new StringReader(xsl)),
                settings);
        }

        public static TransformResults Transform(FileInfo xmlFile, string xsl)
        {
            return Transform(xmlFile, xsl, new TransformSettings());
        }

        public static TransformResults Transform(string xml, string xsl)
        {
            return Transform(xml, xsl, new TransformSettings());
        }

        public static TransformResults Transform(string xml, string xsl,
            TransformSettings settings)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            return Transform(xmlDoc, new XPathDocument(new StringReader(xsl)),
                settings);
        }

        public static TransformResults Transform(XmlDocument xmlDoc, string xsl,
            TransformSettings settings)
        {
            return Transform(xmlDoc, new XPathDocument(new StringReader(xsl)),
                settings);
        }

        public static TransformResults Transform(XmlDocument xmlDoc, string xsl)
        {
            return Transform(xmlDoc, xsl, new TransformSettings());
        }

        public static TransformResults Transform(XmlDocument xmlDoc, XPathDocument xslDoc, 
            TransformSettings settings)
        {
            TransformResults results = new TransformResults();

            // collect any user-supplied or programatically-supplied arguments for transformation
            XsltArgumentList xsltArgs = GetXslTransformArgs(xslDoc, xmlDoc, settings);

            // even if no args, should not be null. for now, use null for cancel-check
            results.Cancelled = (null == xsltArgs);

            // exit if user clicked Cancel button on params form (if shown)
            if (results.Cancelled) return results;

            // create compiled transformation object and load it with our XSL document
            XslCompiledTransform transform = new XslCompiledTransform(settings.EnableDebug);
            transform.Load(xslDoc);

            // setup a buffer to hold results in memory
            MemoryStream resultBuffer = new MemoryStream();
            // and create a writer to write to the results buffer
            StreamWriter resultWriter = new StreamWriter(resultBuffer);

            try
            {
                // run transformation w/specified xml document, args and text writer
                transform.Transform(xmlDoc, xsltArgs, resultWriter);

                // writer filled result buffer; get buffer contents as string
                results.Output = Encoding.UTF8.GetString(resultBuffer.ToArray());                
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                ;
            }

            return results;
        }

        #region Private Methods
        private static XsltArgumentList GetXslTransformArgs(XPathDocument xslDoc,
            XmlDocument xmlDoc, TransformSettings settings)
        {
            // do not initialize to null as null is used to indicate cancel
            XsltArgumentList xsltArgs = new XsltArgumentList();

            if (settings.EnableParamPrompting)
            {
                using (XSLTemplateParams paramsForm = new XSLTemplateParams(xmlDoc, xslDoc))
                {
                    // if there were any xsl params found, then show form to get values
                    if (paramsForm.TemplateArgCount > 0)
                    {
                        paramsForm.TopMost = settings.ShowParamsOnTop;
                        paramsForm.ShowDialog();

                        if (paramsForm.DialogResult != DialogResult.Cancel)
                        {
                            xsltArgs = (paramsForm.TemplateArgCount > 0) ? paramsForm.XsltArgs
                                : new XsltArgumentList();                            
                        }
                        else
                        {
                            // currently used to indicate user clicked Cancel
                            xsltArgs = null;
                        }
                    }
                }
            }

            // if any xsl params were programmatically specified, add them in
            if (null != settings.Parameters && settings.Parameters.Count > 0)
            {
                foreach (KeyValuePair<string, string> item in settings.Parameters)
                {
                    // ensure parameter is not already there
                    if (null == xsltArgs.GetParam(item.Key, string.Empty))
                    {
                        xsltArgs.AddParam(item.Key, string.Empty, item.Value);
                    }
                }
            }

            return xsltArgs;
        }
        #endregion Private Methods
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Geoff Hudik
Software Developer (Senior) TeamHealth
United States United States
No Biography provided
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.141029.1 | Last Updated 13 Jun 2009
Article Copyright 2009 by Geoff Hudik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid