locked
How Can i get a Current Date on XSLT 1.0 RRS feed

  • Question

  • Hi Team,

    Currently we have a requirement to get a current date time to my Xslt 1 map, According to i have used below namespace expression and to get a current dateTime to XML tag

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ex="http://exslt.org/dates-and-times"
    extension-element-prefixes="ex">

    Could you please support to sort out this issue?


    Friday, July 17, 2020 3:00 PM

Answers

  • Hi Mahesh,

    Unfortunately, just XSLT 2.0 and later has built-in functions to do that.

    But there is a workaround. XSLT 1.0 has extensibility mechanism that allows to use .NET Framework power.

    The XSLT below contains c# function that return current DateTime. And it accepts a parameter for its formatting.

    Very powerful.

    XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csharp="urn:my-scripts" exclude-result-prefixes="msxsl csharp">
    	<xsl:output indent="yes" method="xml"/>
    
    	<xsl:template match="/">
    		<root>
    			<CurrentDateTime>
    				<xsl:value-of select="csharp:CurrentDateTime('yyyy-MM-ddTHH:mm:ss.fff')"/>
    			</CurrentDateTime>
    			<CurrentDate>
    				<xsl:value-of select="csharp:CurrentDateTime('yyyy-MM-dd')"/>
    			</CurrentDate>
    			<DefaultDateTime>
    				<xsl:value-of select="csharp:CurrentDateTime('')"/>
    			</DefaultDateTime>
    		</root>
    	</xsl:template>
    
    	<msxsl:script language="C#" implements-prefix="csharp">
    		<msxsl:assembly name="System.Core"/>
    		<msxsl:assembly name="System.Xml.Linq"/>
    		<msxsl:assembly name="System.Linq"/>
    		<msxsl:assembly name="System.Collections"/>
    
    		<msxsl:using namespace="System.Linq"/>
    		<msxsl:using namespace="System.Xml.Linq"/>
    		<msxsl:using namespace="System.Collections.Generic"/>
    	<![CDATA[
       
    	   public string CurrentDateTime(string format)
    	   {
    	   		// default format
    			if (string.IsNullOrEmpty(format)) {
       				format = "yyyy-MM-ddTHH:mm:ss.fffzzz";
    			}
    
    			return DateTime.Now.ToString(format);
    	   }
    
    	]]>
    	</msxsl:script>
    </xsl:stylesheet>

    Output:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <CurrentDateTime>2020-07-17T08:59:18.680</CurrentDateTime>
      <CurrentDate>2020-07-17</CurrentDate>
      <DefaultDateTime>2020-07-17T08:59:18.684-04:00</DefaultDateTime>
    </root>

    • Edited by Yitzhak Khabinsky Friday, July 17, 2020 3:30 PM
    • Proposed as answer by Enric Vives Friday, July 17, 2020 3:50 PM
    • Marked as answer by Maheshma Sunday, July 19, 2020 4:58 PM
    Friday, July 17, 2020 3:26 PM

All replies

  • Hi Mahesh,

    Unfortunately, just XSLT 2.0 and later has built-in functions to do that.

    But there is a workaround. XSLT 1.0 has extensibility mechanism that allows to use .NET Framework power.

    The XSLT below contains c# function that return current DateTime. And it accepts a parameter for its formatting.

    Very powerful.

    XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csharp="urn:my-scripts" exclude-result-prefixes="msxsl csharp">
    	<xsl:output indent="yes" method="xml"/>
    
    	<xsl:template match="/">
    		<root>
    			<CurrentDateTime>
    				<xsl:value-of select="csharp:CurrentDateTime('yyyy-MM-ddTHH:mm:ss.fff')"/>
    			</CurrentDateTime>
    			<CurrentDate>
    				<xsl:value-of select="csharp:CurrentDateTime('yyyy-MM-dd')"/>
    			</CurrentDate>
    			<DefaultDateTime>
    				<xsl:value-of select="csharp:CurrentDateTime('')"/>
    			</DefaultDateTime>
    		</root>
    	</xsl:template>
    
    	<msxsl:script language="C#" implements-prefix="csharp">
    		<msxsl:assembly name="System.Core"/>
    		<msxsl:assembly name="System.Xml.Linq"/>
    		<msxsl:assembly name="System.Linq"/>
    		<msxsl:assembly name="System.Collections"/>
    
    		<msxsl:using namespace="System.Linq"/>
    		<msxsl:using namespace="System.Xml.Linq"/>
    		<msxsl:using namespace="System.Collections.Generic"/>
    	<![CDATA[
       
    	   public string CurrentDateTime(string format)
    	   {
    	   		// default format
    			if (string.IsNullOrEmpty(format)) {
       				format = "yyyy-MM-ddTHH:mm:ss.fffzzz";
    			}
    
    			return DateTime.Now.ToString(format);
    	   }
    
    	]]>
    	</msxsl:script>
    </xsl:stylesheet>

    Output:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <CurrentDateTime>2020-07-17T08:59:18.680</CurrentDateTime>
      <CurrentDate>2020-07-17</CurrentDate>
      <DefaultDateTime>2020-07-17T08:59:18.684-04:00</DefaultDateTime>
    </root>

    • Edited by Yitzhak Khabinsky Friday, July 17, 2020 3:30 PM
    • Proposed as answer by Enric Vives Friday, July 17, 2020 3:50 PM
    • Marked as answer by Maheshma Sunday, July 19, 2020 4:58 PM
    Friday, July 17, 2020 3:26 PM
  • HI Yitzhak,

    Thank you very much using above solution, using .net workaround its worked.

    Thanks

    Mahehs

    Sunday, July 19, 2020 5:00 PM