Posts
53
Comments
71
Trackbacks
27
February 2007 Entries
XSLT - format date

I had someone ask today how to format a date in xslt 1.0.  I thought it was worth posting the answer I gave.  I hope you find it useful.  It's amazing how many lines of code it takes for this, but it's surprising fast. 

convert
a date represented as "Jan-14-2006 08:55:22 (CST)" to "2006-01-14T08:55:22".

XML Sample

<?xml version="1.0" encoding="utf-8"?>
<
doc>
  <
longdate>Jan-14-2006 08:55:22</longdate>
</
doc>

XSLT

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">

    <doc>

      <xsl:element name="newdate">

        <xsl:call-template name="FormatDate">

          <xsl:with-param name="DateTime" select="doc/longdate"/>

        </xsl:call-template>

      </xsl:element>

    </doc>

  </xsl:template>

 

  <xsl:template name="FormatDate">

    <xsl:param name="DateTime" />

    <!-- new date format 2006-01-14T08:55:22 -->

    <xsl:variable name="mo">

      <xsl:value-of select="substring($DateTime,1,3)" />

    </xsl:variable>

    <xsl:variable name="day-temp">

      <xsl:value-of select="substring-after($DateTime,'-')" />

    </xsl:variable>

    <xsl:variable name="day">

      <xsl:value-of select="substring-before($day-temp,'-')" />

    </xsl:variable>

    <xsl:variable name="year-temp">

      <xsl:value-of select="substring-after($day-temp,'-')" />

    </xsl:variable>

    <xsl:variable name="year">

      <xsl:value-of select="substring($year-temp,1,4)" />

    </xsl:variable>

    <xsl:variable name="time">

      <xsl:value-of select="substring-after($year-temp,' ')" />

    </xsl:variable>

    <xsl:variable name="hh">

      <xsl:value-of select="substring($time,1,2)" />

    </xsl:variable>

    <xsl:variable name="mm">

      <xsl:value-of select="substring($time,4,2)" />

    </xsl:variable>

    <xsl:variable name="ss">

      <xsl:value-of select="substring($time,7,2)" />

    </xsl:variable>

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

    <xsl:value-of select="'-'"/>

    <xsl:choose>

      <xsl:when test="$mo = 'Jan'">01</xsl:when>

      <xsl:when test="$mo = 'Feb'">02</xsl:when>

      <xsl:when test="$mo = 'Mar'">03</xsl:when>

      <xsl:when test="$mo = 'Apr'">04</xsl:when>

      <xsl:when test="$mo = 'May'">05</xsl:when>

      <xsl:when test="$mo = 'Jun'">06</xsl:when>

      <xsl:when test="$mo = 'Jul'">07</xsl:when>

      <xsl:when test="$mo = 'Aug'">08</xsl:when>

      <xsl:when test="$mo = 'Sep'">09</xsl:when>

      <xsl:when test="$mo = 'Oct'">10</xsl:when>

      <xsl:when test="$mo = 'Nov'">11</xsl:when>

      <xsl:when test="$mo = 'Dec'">12</xsl:when>

    </xsl:choose>

    <xsl:value-of select="'-'"/>

    <xsl:if test="(string-length($day) &lt; 2)">

      <xsl:value-of select="0"/>

    </xsl:if>

    <xsl:value-of select="$day"/>

    <xsl:value-of select="'T'"/>

    <xsl:value-of select="$hh"/>

    <xsl:value-of select="':'"/>

    <xsl:value-of select="$mm"/>

    <xsl:value-of select="':'"/>

    <xsl:value-of select="$ss"/>

  </xsl:template>

</xsl:stylesheet>

Cheers,

John Workman

posted @ Thursday, February 08, 2007 10:51 PM | Feedback (27)
Essential vocabulary additions for the workplace

I normally don't like to spread spam, but I got these via email yesterday and knew that I would want to refer back to them often.  I hope you enjoy.

1. BLAMESTORMING : Sitting around in a group, discussing why a deadline was missed or a project failed, and who was responsible.
2. SEAGULL MANAGER: A manager, who flies in, makes a lot of noise, craps on everything, and then leaves.
3. ASSMOSIS: The process by which some people seem to absorb success and advancement by kissing up to the boss rather than working hard
4. SALMON DAY : The experience of spending an entire day swimming upstream only to get screwed and die in the end.
5.
CUBE FARM : An office filled with cubicles.
6.
PRAIRIE DOGGING : When someone yells or drops something loudly in a cube farm, and people's heads pop up over the walls to see what's going on.
7.
MOUSE POTATO : The on-line, wired generation's answer to the couch potato.
8.
SITCOMs: Single Income, Two Children, Oppressive Mortgage. What Yuppies get into when they have children and one of them stops working to stay home with the kids.
9.
STRESS PUPPY : A person who seems to thrive on being stressed out and whiny.
10.
SWIPEOUT: An ATM or credit card that has been rendered useless because magnetic strip is worn away from extensive use.
11.
XEROX SUBSIDY : Euphemism for swiping free photocopies from one's workplace.
12.
IRRITAINMENT: Entertainment and media spectacles that are Annoying but you find yourself unable to stop watching them.
13.
PERCUSSIVE MAINTENANCE : The fine art of whacking the crap out of an electronic device to get it to work again. Often feel like doing this to my computer------
14.
ADMINISPHERE : The rarefied organizational layers beginning just above the rank and file. Decisions that fall from the adminisphere are often profoundly inappropriate or irrelevant to the problems they were designed to solve.
15.
404 : Someone who's clueless. From the World Wide Web error Message "404 Not Found," meaning that the requested site could not be located.
16.
GENERICA : Features of the American landscape that are exactly the same no matter where one is, such as fast food joints, strip malls, and subdivisions.
17.
OHNOSECOND : That minuscule fraction of time in which you realize that you've just made a BIG mistake. (Like after hitting send on an email by mistake).
18.
WOOFS : Well-Off Older Folks.
19.
CROP DUSTING : Surreptitiously passing gas while passing through a Cube Farm.
- Author Unknown

posted @ Friday, February 02, 2007 7:00 AM | Feedback (0)
News
Xobni outlook add-in for your inbox