Date and Time

1. Get time and data
2. Insert date and time
1.

Get time and data

Joerg Pietschmann

There is currently no way to do this from within an XSL sheet. I use external tools to write the desired information into a XML file and get the value into the XSL sheet by using document():

Command line frame (requires Unix toolset, Korn shell or bash):

cat >time.xml <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<time>$(date "+%Y-%m-%d %H:%M:%S")</time>
EOF
saxon -o $3 $1 $2

XSL snippet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="time" select="document('time.xml')/time"/>
...

This reads a nicely formatted timestamp into the $time variable. If you need year, month, day etc. separately, you could structure the time.xml file:

cat >time.xml <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<time>
  <year>$(date "+%Y")</year>
  <month>$(date "+%m")</month>
   ...
</time>
EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="time" select="document('time.xml')/time"/>

  ... 
    <my-year><xsl:value-of select="$time/year"/></my-year>
  ...

Of course, if you work on Windows, creating time.xml may be a greater challenge. I installed an Unix toolset on my windows machine (the example above was developed on windows!). There are several, you might want to check out the free CYGWIN toolkit at http://www.cygwin.org . Alternatively, you can use WSH, Visual Basic, Perl or write a java program.

2.

Insert date and time

???

 * generate a timestamp (this is a M$-DOS example)
 echo %date% %time% >timestamp.txt
 * declare this file as an entity:
 <!DOCTYPE xsl:stylesheet[
 ...
 <!ENTITY timestamp SYSTEM "timestamp.txt">
 ...
 ]>
 * and use it
 <pubdate>&timestamp;</pubdate>