using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace SharePointOfView.Utilities
{
///
/// SharePointOfView XSLT Utility class
///
public static class XSLT
{
///
/// Perform an XSL Transformation using XML and XSL content
///
/// XML source
/// XSL source
/// HTML
public static string XSLTransform(string xmlContent, string xsltContent)
{
return XSLTransform(xmlContent, xsltContent, null);
}
///
/// Perform an XSL Transformation using XML and XSL content and with some XSLT arguments
///
/// XML source
/// XSL source
/// XSLT argument list
/// HTML
public static string XSLTransform(string xmlContent, string xsltContent, XsltArgumentList xsltArgs)
{
string s;
try
{
// Initialize a memorystring for output
MemoryStream mem = new MemoryStream();
// Initialize a StreamReader using the MemoryStream
StreamReader sr = new StreamReader(mem, UTF8Encoding.UTF8);
// Initialize an XSLTransform object
XslTransform xTrans = new XslTransform();
// Load XML Content
XmlTextReader xmlReader = new XmlTextReader(xmlContent, XmlNodeType.Document, null);
XPathDocument xmlDoc = new XPathDocument(xmlReader);
// Load XSL Content
XmlTextReader xslReader = new XmlTextReader(xsltContent, XmlNodeType.Document, null);
XPathDocument xslDoc = new XPathDocument(xslReader);
// Load XSL into transform
xTrans.Load(xslDoc.CreateNavigator());
// Perform transformation
xTrans.Transform(xmlDoc.CreateNavigator(), xsltArgs, mem);
// Copy stream to string
mem.Position = 0;
s = sr.ReadToEnd();
sr.Close();
mem.Close();
}
catch (Exception ex)
{
s = ex.Message;
}
return s;
}
}
}