CustomDate custDate = new CustomDate() ;
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myCustDate="urn:custDate">
xslArgs.AddExtensionObject("urn:custDate", custDate) ;
<xsl:value-of select="myCustDate:GetDateDiff(./joiningdate)"/>
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 doc XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ; XslTransform myXslTrans = new XslTransform() ; //load the Xsl myXslTrans.Load(sXslPath) ; XsltArgumentList xslArgs = new XsltArgumentList() ; //create custom object CustomDate custDate = new CustomDate() ; //pass an instance of the custom object xslArgs.AddExtensionObject("urn:custDate", custDate) ; //create the output stream XmlTextWriter myWriter = new XmlTextWriter("extendXSLT.html", null) ; //pass the args,do the actual transform of Xml myXslTrans.Transform(myXPathDoc,xslArgs, myWriter) ; myWriter.Close() ; }catch(Exception e){ Console.WriteLine("Exception: {0}", e.ToString()); } } public static void PrintUsage(){ Console.WriteLine("Usage: XsltExtension.exe <xml path> >xsl path<") ; } } //our custom class public class CustomDate{ //function that gets called from XSLT public string GetDateDiff(string xslDate){ DateTime dtDOB = DateTime.Parse(xslDate) ; DateTime dtNow = DateTime.Today ; TimeSpan tsAge = dtNow.Subtract(dtDOB) ; return tsAge.Days.ToString() ; } }
Build Your Own ASP.NET Website Using C# & VB.NET