Developer42

2010-06-24

Getting Todays Date from an XSLT Transform

Filed under: Microsoft, Technology — Developer42 @ 17:38

Today I hit an issue whereby I needed to put the current date into an XML document, but found that the current-datetime() function wasn’t supported by my xsl processor. As a result, I’ve written the below code to get around the issue (heavily based on resources found elsewhere on the web). Please see the comments inline for information on what each bit’s doing.
One extra point is that this will only work if scripts are available to your XSLT processor. If you’re using C#, please lookup XsltSettings.EnableScripts for more info.


<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:jsGoodies="urn:internal:jsGoodies">
  <!-- nb: the msxsl and jsGoodies namespaces above are required for the date function.  The ms one allows me to use scripts, the jsgoodies references my script, below -->
  <!-- dont indent; improves processing speed - but leave as yes for debugging - also strip all whitespace if not debugging, as this will help speed and file size -->
  <xsl:output method="xml" indent="yes" encoding="utf-16" />
  <!-- <xsl:strip-space elements="*" /> -->
< put the functions result into a variable to save reprocessing each time its referenced >
  <xsl:variable name="now" select="jsGoodies:GetCurrentDateTime()" />
  <xsl:template match="/node()">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
          <xsl:element name="Now">
        <!-- use xsl:element instead of just &lt;Now&gt; to avoid getting namespace fluff -->
          <xsl:value-of select="$now"/>
          </xsl:element>
      <xsl:apply-templates select="*"/>
    </xsl:copy>
  </xsl:template>

  <!-- By default, copy everything as is -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

    <!-- 
    current-date() is not available in the current MS version of XSLT.  Therefore using a JScript hack
    http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/d8c0b688-e176-4316-b70a-637e2bfcf6dc/ 
    -->  
    <msxsl:script language="JScript" implements-prefix="jsGoodies">
      <![CDATA[
        //cdata tells xml parser to ignore this bit, allowing me to do things like this "i > u" & that <--.  Very useful when writing code 🙂
        function GetCurrentDateTime()
        {
         var result = new Date(); 
         return ToXmlDate(result);
        }
        function ToXmlDate(d)
        {
          //I'm using the UTC date to avoid timezone issues
          return d.getUTCFullYear()
          + '-' + ToMonth(d.getUTCMonth())
          + '-' + LPad(d.getUTCDate(),'0',2)
          + 'T' + LPad(d.getUTCHours(),'0',2)
          + ':' + LPad(d.getUTCMinutes(),'0',2)
          + ':' + LPad(d.getUTCSeconds(),'0',2)
          + '.' + LPad(d.getUTCMilliseconds(),'0',3)
          + 'Z';
        }
        function ToMonth(i)
        {
          i++; //the month is given as a zero based index, rather than the month value
          return LPad(i,'0',2);
        }
        function LPad(s,c,l)
        {
          return (""+s).PadLeft(l,c); //ensures that s is a string, then uses the strings padleft function
        }
    ]]>
    </msxsl:script>

</xsl:stylesheet>   
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: