How To Compare Two Dates In XSLT (Transform) By Using C# Code

Introduction

This blog gives us an overall idea of how to compare two dates in XSLT. There may be some cases when we need to add this functionality in XSLT to compare the dates while transforming an XML document to another format. Such things cannot be done in XSLT directly due to the lack of manipulation functions. In order to cure that, Microsoft has provided a way to include C# code directly in XSLT.

Background

In this blog, we will see how we can implement the date comparison functionality in XSLT. Below are the parameters.
  • Input – XML file with Date element.
  • Transform – XSLT: Added C# function to compare date.
  • Output - XML File
So basically, we can write C# code in XSLT. We are going to create one C# method that compares two dates. I have created C# method that compares two dates and returns a result.

Syntax

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ClassNameHere="dummy namespace here">  
  3.     <msxsl:script language="C#" implements-prefix="ClassNameHere">  
  4.         <![CDATA[  
  5.   
  6. public string MethodName(/* some parameters here*/)  
  7.   
  8. {  
  9.   
  10. return /*something*/;  
  11.   
  12. }  
  13.   
  14. ]]>  
  15.     </msxsl:script>  
  16.     <xsl:template match="//Some Node">  
  17.         <xsl:value-of select="ClassNameHere:MethodName(SomeNodeHere)" /> </xsl:template>  
  18. </xsl:stylesheet>  

Let's create a sample console application to see how it works.

Open Visual Studio and go to File - New Project - Console Application; give it a suitable name, such as - ‘XSLTDateComapre’ and click OK.



Let's create a main XML, i.e., input.xml. Right click on ‘Solution Explorer’ - Add - New Item and select XML file; give it a name like input.xml.

XML

XML Code 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <note>  
  3.     <from>sagar@gmail.com</from>  
  4.     <to>swapnil@gmail.com</to>  
  5.     <cc>pankaj@gmail.com</cc>  
  6.     <bcc>amit@gmail.com</bcc>  
  7.     <heading1>Reminder</heading1>  
  8.     <heading2>Important</heading2>  
  9.     <UpdatedDate>01/06/2018</UpdatedDate>  
  10.   
  11.     <body>Don't forget me this weekend!</body>  
  12. </note>  
Now, create an XSLT transform file to match the above requirement. Right-click on ‘Solution Explorer’ - Add - New Item and select XSLT File and give it a name like transform.xslt.

Code 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl cs" xmlns:cs="urn:cs">  
  3.     <msxsl:script language="C#" implements-prefix="cs">  
  4.         <![CDATA[  
  5.   
  6. public bool chkdate(string inputDate)  
  7.   
  8. {  
  9.   
  10. bool rtn=false;  
  11.   
  12. if(DateTime.Parse(inputDate)>DateTime.Parse(DateTime.Now.Date.ToString("MM/dd/yyyy")))  
  13.   
  14. {  
  15.   
  16. rtn=true;  
  17.   
  18. }  
  19.   
  20. return rtn;  
  21.   
  22. }  
  23.   
  24. ]]>  
  25.     </msxsl:script>  
  26.     <xsl:output method="xml" indent="yes" />  
  27.     <xsl:template match="/">  
  28.         <Details>  
  29.             <From>  
  30.                 <xsl:value-of select="note/from" /> </From>  
  31.             <To>  
  32.                 <xsl:value-of select="note/to" /> </To>  
  33.             <Heading>  
  34.                 <xsl:value-of select="concat(note/heading1,', ',note/heading2)" /> </Heading>  
  35.             <IsUpdatedDateGreaterThanCurrentDate>  
  36.                 <xsl:value-of select="cs:chkdate(note/UpdatedDate)" /> </IsUpdatedDateGreaterThanCurrentDate>  
  37.         </Details>  
  38.     </xsl:template>  
  39. </xsl:stylesheet>  
C# Code
  1. string baseName = System.Environment.CurrentDirectory;  
  2. XslTransform myXslTransform;  
  3. myXslTransform = new XslTransform();  
  4. myXslTransform.Load(baseName + "\\trasform.xslt");  
  5. myXslTransform.Transform(baseName + "\\input.xml", baseName + "\\output.xml");   
Output – [Input Date -01/07/2018 an Sys Date- 01/06/2018]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Details>  
  3.     <From>sagar@gmail.com</From>  
  4.     <To>swapnil@gmail.com</To>  
  5.     <Heading>Reminder, Important</Heading>  
  6.     <IsUpdatedDateGreaterThanCurrentDate>true</IsUpdatedDateGreaterThanCurrentDate>  
  7. </Details>  
Conclusion

I believe, you have got an idea of adding C# code in XSLT and I hope you enjoyed this blog. Your valuable feedback, questions, or comments about this write-up are always welcome.