Question

Date Format in XSLT

Asked by: kermitastic

Hello,

I just started learning XSLT so please bear with me. Is there an easy way to format the date node below to something like "16-09-2007" removing all the extra information? I would need this to sort by date...

<files>
  <date>Mon, 16 Sep 2007 17:54:26 +0000</date>
  <enclosure file="custom_001.wmv />
</files>

Thanks..

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-08-19 at 19:59:32ID22773206
Tags

date

,

format

,

xslt

Topics

Extensible Stylesheet Language Transformation (XSLT)

,

Extensible Markup Language (XML)

Participating Experts
2
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. XSLT
    I'm using XSLT to transform an XML document into a new XML document. What I would like to know is if there is any way that I can perform some text manipulation on the contents of an element. Fore example, let's say I've got the following XML element: <time>2001-0...
  2. XSLT
    HI all, I am trying to srite a simple xslt to list the titles of scientific journals artcles. This is not working however and I could really do with a point in the right direction. I have pasted in both the xml nd xslt so far below. Currently every field is being listed, rat...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: naik_sanketPosted on 2007-08-19 at 20:19:50ID: 19728110

http://www.experts-exchange.com/Web/Web_Languages/XML/Q_20844888.html

You might like to have a look at this previous post. Hope it solves your problem. Do let know if your requirement is someting else.

Regards,
Sanket

 

by: kermitasticPosted on 2007-08-19 at 20:44:22ID: 19728166

Hello,

Checked it out but the code, but didnt work, pretty sure Im missing something. Please see below (see a Ja// instead).

<xsl:template match="/">
     <xsl:call-template name="showDateDropdown" />
  </xsl:template>

<xsl:template name="showDateDropdown">
    <xsl:call-template name="format-date">
      <xsl:with-param name="date" select="date" />
      <xsl:with-param name="format" select="1" />
    </xsl:call-template>
    <form name='drop2'>
      <select style="width:180px; background:#ccc; color:#000; font-weight:bold; font-size:10px;">
        <xsl:attribute name="onChange">
          <xsl:text>this.form.submit();</xsl:text>
        </xsl:attribute>
        <option value='0'>Select Date</option>
        <xsl:for-each select="//item[generate-id() = generate-id(key('item-by-date', date) [1])]">
          <option value='{date}'>
            <xsl:value-of select="date"/>
          </option>
        </xsl:for-each>
      </select>
    </form>
  </xsl:template>


 <!-- 1/8/04  01.08.04   Jan 8 2004 -->
  <xsl:template name="format-date">
    <xsl:param name="date" />
    <xsl:param name="format" select="0" />

    <xsl:variable name="day" select="substring-before(substring-after($date, ' '), ' ')" />
    <xsl:variable name="monthName" select="substring-before(substring-after(substring-after($date, ' '), ' '), ' ')" />
    <xsl:variable name="year" select="substring-before(substring-after(substring-after(substring-after($date, ' '), ' '), ' '), ' ')" />

    <xsl:variable name="month" select="substring(substring-after('Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12', $monthName), 1, 2)" />
    <xsl:variable name="day2" select="concat(translate(substring($day,1,1), '0', ''), substring($day,2,1))" />
    <xsl:variable name="month2" select="concat(translate(substring($month,1,1), '0', ''), substring($month,2,1))" />

    <xsl:choose>
      <xsl:when test="$format = 1">
        <xsl:value-of select="concat($month2, '/', $day2, '/', substring($year, 3))" />
      </xsl:when>
      <xsl:when test="$format = 2">
        <xsl:value-of select="concat($month, '.', $day, '.', substring($year, 3))" />
      </xsl:when>
      <xsl:when test="$format = 3">
        <xsl:value-of select="concat($monthName, ' ', $day2, ' ', $year)" />
      </xsl:when>
      <xsl:when test="$format = 4">
        <xsl:value-of select="concat($day2, '/', $month2, '/', substring($year, 3))" />
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="$date" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

 

by: kermitasticPosted on 2007-08-19 at 20:45:28ID: 19728169

Sorry, let me rephrase that in Englist :-)

*** Checked itout but the code didnt work, pretty sure Im missing something. Please see below (see a Ja// instead) ***

 

by: GertonePosted on 2007-08-20 at 00:12:59ID: 19728627

well, the code does work (though it doesn't provide the format you need),
 but you pass it the wrong context.
In this limited example you need to pass the files context as well
        <xsl:call-template name="format-date">
            <xsl:with-param name="date" select="files/date" />

note that with
<xsl:template match="/">
     <xsl:call-template name="showDateDropdown" />
  </xsl:template>
you pass the current context... and date is not a direct child of the root

now about the format-date template itself
first I am not really fond of the long series of substring-after manipulations,
specially not when there is reuse (if you have to do the same calculation twice, store the result in a variable)
but this is a minor detail

the format1 is not the format you need,
if you want to use the date-string in a sort, you don' have to strip off the "0"s, you want them for good sorting
your question was "16-09-2007"
the format 1 gives you 9/16/07 which is useless for sorting
but if you want to use the date for sorting, you will need 2007-09-16
assuming that that is what you want, I rewrote the format-date template

It seems like you are going to do a lot of on-and-off conversions, so I stripped off the options
This format date template only does 2007-09-16 as an output

    <xsl:template name="format-date">
        <xsl:param name="date"></xsl:param>
        <xsl:variable name="month-to-mm">Jan01JAN01Feb02FEB02Mar03MAR03Apr04APR04May05MAY05Jun06JUN06Jul07JUL07Aug08AUG08Sep09SEP09Oct10OCT10Nov11NOV11Dec12DEC12</xsl:variable>
       
        <xsl:variable name="dayless" select="substring-after($date, ' ')"></xsl:variable>
        <xsl:variable name="day" select="substring-before($dayless, ' ')" />
        <xsl:variable name="ddless" select="substring-after($dayless, ' ')"></xsl:variable>
        <xsl:variable name="monthName" select="substring-before($ddless, ' ')" />
        <xsl:variable name="year" select="substring-before(substring-after($ddless, ' '), ' ')" />
       
        <xsl:variable name="month" select="substring(substring-after($month-to-mm, $monthName), 1, 2)" />
        <xsl:variable name="day2">
                <xsl:if test="string-length($day) = 1">
                    <xsl:text>0</xsl:text>
                </xsl:if>
            <xsl:value-of select="$day"/>
        </xsl:variable>
         <xsl:value-of select="concat($year, '-', $month, '-', $day2)" />
    </xsl:template>

this one only takes one parameter

finally you want the dates in the dropdown list I assume, so not before it

Looking at your dropdown
I see from which question you took the dropdown code (are you a colleague of merdelafuente?)
but it hardly makes sense in your example
you are by the way using a key you did not declare

If you really need unique dates in the dropdown, you have to make a key
but then I strongly suggest that you transform all the dates first, if you can.
Can you change your source XML, so that the dates are in a sortable format?
(by doing an identity transform together with the format-date template)
If you need to keep the original date we could always add an attribute, like this
<files>
  <date yyyymmdd="2007-09-16">Mon, 16 Sep 2007 17:54:26 +0000</date>
  <enclosure file="custom_001.wmv />
</files>
Adding the preformatted date in the source XML, one way or another,
will make live a lot easier in the sorting and dropdown stage

let me knwo any further requirements

Geert



 

by: kermitasticPosted on 2007-08-20 at 07:22:31ID: 19730551

Hello Geert

Ahh that makes sense. Yes I saw that dropdown example here from EE  (in which you were one of the responders) It happens that i am working off of an XML from a 3rd party that I cannot change. I will give your example a try and will let you know.

Thx!

 

by: GertonePosted on 2007-08-20 at 07:25:56ID: 19730580

please note that you can have two steps in working with the XML... one step that transforms the source dates to a sortable form
and another step building the dropdown and doing the sorting

cheers

Geert

 

by: kermitasticPosted on 2007-08-20 at 09:08:04ID: 19731449

Hello Gertone

Your example worked great! I was able to build the dropdown with the unique keys -- one last question before I close the question if you dont mind?

Where do I actually call the sort statement? The display dropdown template or the main template? Here's the code that im working off of:

...
       <xsl:for-each select="//item[generate-id() = generate-id(key('item-by-date', date) [1])]">
          <xsl:sort select="date" order="descending"/>
          <option>            
            <xsl:attribute name="value">
              <xsl:call-template name="format-date">
                <xsl:with-param name="date" select="date" />
                </xsl:call-template>          
             </xsl:attribute>
            <xsl:value-of select="title"/>
          </option>
        </xsl:for-each>

I really, really appreciate your help :o)

 

by: GertonePosted on 2007-08-20 at 09:21:50ID: 19731538

That is the main problem of trying to sort the non normalised dates
There is no simple function that you can use in the select of the sort
(sort needs to be written as a first line in the for-each)
this means you will end up with some complex sort statements in the for-each
(almost rebuilding the entire named template again)

    <xsl:for-each select="//item[generate-id() = generate-id(key('item-by-date', date) [1])]">
        <xsl:sort select="substring-before(substring-after(substring-after(substring-after(date, ' '), ' '), ' '), ' ')" order="descending" data-type="number" /> <!-- sorts the year -->
        <xsl:sort select="substring(substring-after('Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12', substring-before(substring-after(substring-after(date, ' '), ' '), ' ')), 1, 2)" order="descending" data-type="number" /> <!-- yuck... sorts the month -->
        <xsl:sort select="substring-before(substring-after(date, ' '), ' ')" order="descending" data-type="number" /> <!-- sorts the day-->
        <option>            
            <xsl:attribute name="value">
                <xsl:call-template name="format-date">
                    <xsl:with-param name="date" select="date" />
                </xsl:call-template>          
            </xsl:attribute>
            <xsl:value-of select="title"/>
        </option>
    </xsl:for-each>

yes, this is a real pain to maintain and debug
hence my suggestion to transform it first to a better date format and work on the new XML document

cheers

Geert

 

by: kermitasticPosted on 2007-08-20 at 09:33:05ID: 19731623

Wow. I'll see if I can sort with something else.

Thanks again Geert Im sure you'll see me around!

 

by: GertonePosted on 2007-08-20 at 09:51:25ID: 19731747

welcome

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...