Current Date and Time in XSLT (DOS/Windows)

Version

Date

Changes

1.0

2009-06-12

Initial version.

Description

This topic shows a way to retrieve the current date or time from DOS when using ePublisher 9.2.

The method in this topic uses DOS (Windows) batch files to get the date or time from Windows.

getdate.bat

@echo off
date /T

gettime.bat

@echo off
time /T

Place these two batch files in the a folder called Tools in the format directory, like this:

Formats
   Dynamic HTML
      Pages
      Tools
      Transforms

Call one of the batch files and return the date or time like this:

<xsl:variable name="GetDateBatchFile">
  <xsl:value-of select="wwfilesystem:Combine( wwprojext:GetProjectFormatDirectoryPath(), 'tools', 'getdate.bat' )" />
</xsl:variable>
<xsl:choose>
 <xsl:when test="wwfilesystem:Exists( $GetDateBatchFile )" >
     <xsl:value-of select="wwstring:ReplaceWithExpression( 
                 wwstring:ReplaceWithExpression( wwexec:ExecuteCommand( $GetDateBatchFile ), '[\n\r]', '' ), ' $','')" />
 </xsl:when>
 <xsl:otherwise>
     <xsl:variable name="VarLogDate" select="wwlog:Error('Could not determine current date, using default 01/01/2007 instead.')" />
     <xsl:value-of select="'01/01/2007'" />
 </xsl:otherwise>
</xsl:choose>

Utility file

I use a utility XSLT for storing templates of common functions. This file has templates for getting the current date, time, and year. Then I can just call one of the functions when I need a date or time, like this:

<xsl:include href="wwformat:Transforms/utility.xsl" />
<xsl:variable name="GlobalCurrentDate" >
  <xsl:call-template name="GetCurrentDate" />
</xsl:variable>
<xsl:variable name="GlobalCurrentYear">
  <xsl:call-template name="GetCurrentYear" />
</xsl:variable>

utility.xsl

Here is the complete listing for utility.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="urn:WebWorks-Reports-Schema"
                              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:proj_cache="http://tempuri.org/ProjectCache.xsd"
                              xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                              xmlns:context="urn:WebWorks-Reports-Schema"
                              xmlns:wwreport="urn:WebWorks-Reports-Schema"
                              xmlns:wwsplits="urn:WebWorks-Engine-Splits-Schema"
                              xmlns:wwlinks="urn:WebWorks-Engine-Links-Schema"
                              xmlns:wwlocale="urn:WebWorks-Locale-Schema"
                              xmlns:wwmode="urn:WebWorks-Engine-Mode"
                              xmlns:wwfiles="urn:WebWorks-Engine-Files-Schema"
                              xmlns:wwbehaviors="urn:WebWorks-Behaviors-Schema"
                              xmlns:wwdoc="urn:WebWorks-Document-Schema"
                              xmlns:wwproject="urn:WebWorks-Publish-Project"
                              xmlns:wwpage="urn:WebWorks-Page-Template-Schema"
                              xmlns:wwprogress="urn:WebWorks-XSLT-Extension-Progress"
                              xmlns:wwlog="urn:WebWorks-XSLT-Extension-Log"
                              xmlns:wwfilesystem="urn:WebWorks-XSLT-Extension-FileSystem"
                              xmlns:wwuri="urn:WebWorks-XSLT-Extension-URI"
                              xmlns:wwstring="urn:WebWorks-XSLT-Extension-StringUtilities"
                              xmlns:wwfilesext="urn:WebWorks-XSLT-Extension-Files"
                              xmlns:wwprojext="urn:WebWorks-XSLT-Extension-Project"
                              xmlns:wwexsldoc="urn:WebWorks-XSLT-Extension-Document"
                              xmlns:wwexec="urn:WebWorks-XSLT-Extension-Execute"
                              xmlns:wwenv="urn:WebWorks-XSLT-Extension-Environment"
                              exclude-result-prefixes="xsl proj_cache msxsl wwsplits wwlinks wwlocale wwmode wwfiles wwbehaviors wwdoc wwproject wwpage wwprogress wwlog wwfilesystem wwuri wwstring wwfilesext wwprojext wwexsldoc wwexec wwenv"
>


 <xsl:template name="GetCurrentDate">
    <!-- TODO: When ePub supports XSL version 2.0, use current-dateTime() instead.
      <xsl:variable name="Foo" select="current-dateTime()" />
    -->
   <xsl:variable name="GetDateBatchFile">
     <xsl:value-of select="wwfilesystem:Combine( wwprojext:GetProjectFormatDirectoryPath(), 'tools', 'getdate.bat' )" />
   </xsl:variable>
   <xsl:choose>
    <xsl:when test="wwfilesystem:Exists( $GetDateBatchFile )" >
        <xsl:value-of select="wwstring:ReplaceWithExpression( wwstring:ReplaceWithExpression( wwexec:ExecuteCommand( $GetDateBatchFile ), '[\n\r]', '' ), ' $','')" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="VarLogDate" select="wwlog:Error('Could not determine current date, using default 01/01/2007 instead.')" />
        <xsl:value-of select="'01/01/2007'" />
    </xsl:otherwise>
   </xsl:choose>
   <!-- TODO: Notes to consider for future improvements.  Date format should be: MMM DD, YYYY
   substring("JanFebMarApr....", ($month-1)*3+1, 3)

   <xsl:variable name="date" select="'1-12-2003'"/>
   <xsl:value-of select="concat(substring-before($date,'-'), '-', substring('JanFebMarAprMayJunJulAugSebOctNovDec',substring-before(substring-after($date,'-'),'-')*3-2,3), '-', substring-after(substring-after($date,'-'),'-'))"/>
   -->
 </xsl:template>

 <xsl:template name="GetCurrentYear">
   <xsl:variable name="LocalCurrentDate">
     <xsl:call-template name="GetCurrentDate" />
   </xsl:variable>
   <xsl:value-of select="wwstring:ReplaceWithExpression( $LocalCurrentDate, '^.*/', '')" />
 </xsl:template>


 <xsl:template name="GetCurrentTime">
    <!-- TODO: When ePub supports XSL version 2.0, use current-dateTime() instead.
      <xsl:variable name="Foo" select="current-dateTime()" />
    -->
   <xsl:variable name="GetTimeBatchFile">
     <xsl:value-of select="wwfilesystem:Combine( wwprojext:GetProjectFormatDirectoryPath(), 'tools', 'gettime.bat' )" />
   </xsl:variable>
   <!-- TODO: Finish this -->
   <xsl:choose>
    <xsl:when test="wwfilesystem:Exists( $GetTimeBatchFile )" >
        <xsl:value-of select="wwstring:ReplaceWithExpression( wwstring:ReplaceWithExpression( wwexec:ExecuteCommand( $GetTimeBatchFile ), '[\n\r]', '' ), ' $','')" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="VarLogTimeError" select="wwlog:Error('Could not determine current time, using default 12:34 instead.')" />
        <xsl:value-of select="'12:34'" />
    </xsl:otherwise>
   </xsl:choose>
  </xsl:template>

  </xsl:template>

</xsl:stylesheet>


CategoryTemplate CategorySolutionsOutputXmlAndXsl

DevCenter/Projects/Current Date and Time in XSLT (DOS) (last edited 2009-06-12 22:32:17 by MikeHedblom)