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
- <?xml version="1.0" encoding="utf-8" ?>
- <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">
- <msxsl:script language="C#" implements-prefix="ClassNameHere">
- <![CDATA[
-
- public string MethodName()
-
- {
-
- return ;
-
- }
-
- ]]>
- </msxsl:script>
- <xsl:template match="//Some Node">
- <xsl:value-of select="ClassNameHere:MethodName(SomeNodeHere)" /> </xsl:template>
- </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 Code
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
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <msxsl:script language="C#" implements-prefix="cs">
- <![CDATA[
-
- public bool chkdate(string inputDate)
-
- {
-
- bool rtn=false;
-
- if(DateTime.Parse(inputDate)>DateTime.Parse(DateTime.Now.Date.ToString("MM/dd/yyyy")))
-
- {
-
- rtn=true;
-
- }
-
- return rtn;
-
- }
-
- ]]>
- </msxsl:script>
- <xsl:output method="xml" indent="yes" />
- <xsl:template match="/">
- <Details>
- <From>
- <xsl:value-of select="note/from" /> </From>
- <To>
- <xsl:value-of select="note/to" /> </To>
- <Heading>
- <xsl:value-of select="concat(note/heading1,', ',note/heading2)" /> </Heading>
- <IsUpdatedDateGreaterThanCurrentDate>
- <xsl:value-of select="cs:chkdate(note/UpdatedDate)" /> </IsUpdatedDateGreaterThanCurrentDate>
- </Details>
- </xsl:template>
- </xsl:stylesheet>
C# Code
- string baseName = System.Environment.CurrentDirectory;
- XslTransform myXslTransform;
- myXslTransform = new XslTransform();
- myXslTransform.Load(baseName + "\\trasform.xslt");
- myXslTransform.Transform(baseName + "\\input.xml", baseName + "\\output.xml");
Output – [Input Date -01/07/2018 an Sys Date- 01/06/2018]
- <?xml version="1.0" encoding="utf-8"?>
- <Details>
- <From>[email protected]</From>
- <To>[email protected]</To>
- <Heading>Reminder, Important</Heading>
- <IsUpdatedDateGreaterThanCurrentDate>true</IsUpdatedDateGreaterThanCurrentDate>
- </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.