Posts Tagged ‘compiledtransform’

XML is a universal format for data exchange, most of the data repository owners store their data in xml format, and apply XSLT to transform the XML from one format to another when required, I came across to a similar project, where we were storing everything in XML and then while processing the data we needed to display in other format.

Have written my own small transformation utility with extension object support.

Transform method:

/// <summary>
/// perform the XSLT transformation on the input xml and provides the transformed xml
/// </summary>
/// <param name=”transformation”>contains input xml, xslt path, and output xml</param>
public static void Transform(ITransformation transformation)
{
string sourceXML = transformation.SourceXML;
using (XmlReader reader = XmlReader.Create(new StringReader(sourceXML)))
{
//instead of initializing it with a new instance every time, get the transformation from the static cache.
//it will improve the load time of transformation object
XslCompiledTransform xslt = PrepareTransformationObject(transformation.XSLTName);

XsltArgumentList args = new XsltArgumentList();
//adding extension object, it will work as a helper method from XSLT
args.AddExtensionObject(“urn:Util”, new Util(transformation.XsltParameters));

//passing the current date as transformation date, you can pass any object and then access it in util class.
XsltParam xsltParam = new XsltParam();
xsltParam.Params.Add(“TransactionDate”, DateTime.Now.ToShortDateString());