All about XML transformation using XSLT and C#

Posted: April 13, 2014 in CodeProject
Tags: , , , , , ,

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));

StringWriter stringWriter = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(stringWriter);

//applying tranform
xslt.Transform(reader, args, writer);

//set the output xml
transformation.DestinationXML = stringWriter.ToString();
}
}

 

This method stores the transformation object in static cache, so next time if you are using the same XSLT it will retrieve the object from cache instead of loading it again.

static Hashtable _xsltText = new Hashtable();

/// <summary>
/// Return the tranformation object, either create new or get from static cache if already available
/// </summary>
/// <param name=”transformationName”>full path of xslt</param>
/// <returns></returns>
static XslCompiledTransform PrepareTransformationObject(string transformationName)
{
if (_xsltText.ContainsKey(transformationName))
return (XslCompiledTransform)_xsltText[transformationName];
else
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(transformationName);
if (!_xsltText.ContainsKey(transformationName))
{
//you can lock the object if you face the concurrency issues
//lock (lockObj)
//{
_xsltText.Add(transformationName, xslt);
//}
}
return xslt;
}
}

 

Example using the above methods:

string xsltName = Server.MapPath(@”XSLT\Sample.xslt”);
string inputXML = XElement.Load(Server.MapPath(@”XML\input.xml”)).ToString();
string outputXML = string.Empty;

//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());

//prepare the tranformation container
TransformationContainer container = new TransformationContainer(xsltName, inputXML, outputXML, xsltParam);

TransformationHelper.Transform(container);

outputXML = container.DestinationXML;
XElement.Parse(outputXML).Save(Server.MapPath(@”XML\output.xml”));

Response.Write(outputXML);

 

Download the complete source XML-XSLT-TransformationSample.zip

 

Cheers!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s