subscribe: Posts | Comments

Extending XSLT using C#

0 comments

XSLT is a transformation language for XML. It allows server systems to transform the source XML tree into a more suitable form for clients, which can all be learned at http://www.learnit.com/visualstudio/. XSLT uses node patterns to match against templates to perform its transformations. Though it makes comple transformations relatively simple there are some situations where we might have to use some custom classes.

Some of the situations where we might need to extend XSLT are:

1) Call custom business logic
2) Perform different actions depending on Permissions
3) Perform complex formatting for dates, strings etc
4) Or even call a webservice!!

Steps to extend XSLT

1) Create the custom object to use from within XSLT(in C#)

CustomDate custDate = new CustomDate() ;
2) Provide a custom namespace declaration for the custom class
 within XSLTs namespace declaration(in XSLT file) 
<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myCustDate="urn:custDate">
3) Pass an instance of the custom object to XSLT, with the same namespace 
as in last step(in C#) 
xslArgs.AddExtensionObject("urn:custDate", custDate) ;
4) Use the object from within XSLT(in XSLT file) 
<xsl:value-of select="myCustDate:GetDateDiff(./joiningdate)"/>
Sample code
For our example let us assume we have a XSLT sheet where we need to manipulate dates. 
We need to show the number of days the employee has been with the company. 
Since XSLT has no native date manipulation functions, 
let us use an extension object for our task. using System ;
using System.IO ;
using System.Xml ;
using System.Xml.Xsl ;
using System.Xml.XPath ;
public class XsltExtension{
public static void Main(string[] args){
if (args.Length == 2){
Transform(args[0], args[1]) ;
}else{
PrintUsage() ;
}
}
public static void Transform(string sXmlPath, string sXslPath){
try{
//load the Xml docnew XPathDocument(sXmlPath) ;
XslTransform myXslTrans = new XslTransform() ;
//load the Xsl new XsltArgumentList() ;
//create custom objectnew CustomDate() ;
//pass an instance of the custom object
//create the output streamnew XmlTextWriter("extendXSLT.html", null) ;
//pass the args,do the actual transform of Xmlcatch(Exception e){
Console.WriteLine("Exception: {0}", e.ToString());
}     
}   
public static void PrintUsage(){
Console.WriteLine("Usage: XsltExtension.exe <xml path> >xsl path<") ;
}
}
//our custom classpublic class CustomDate{    
//function that gets called from XSLTpublic string GetDateDiff(string xslDate){        
DateTime dtDOB = DateTime.Parse(xslDate) ;        
DateTime dtNow = DateTime.Today ;        
TimeSpan tsAge = dtNow.Subtract(dtDOB) ;        
return tsAge.Days.ToString() ;
}
}
XPathDocument myXPathDoc = 
myXslTrans.Load(sXslPath) ;
XsltArgumentList xslArgs = 
CustomDate custDate = 
xslArgs.AddExtensionObject("urn:custDate", custDate) ;
XmlTextWriter myWriter = 
myXslTrans.Transform(myXPathDoc,xslArgs, myWriter) ;        
myWriter.Close() ;
}