hwg-languages archives | Aug 2002 | new search | results | previous | next |
RE: XSLby "nik krimm" <nik(at)krimm.net> |
|
Rob: You can use the concept of called-templates, and the xsl funtions substring-after and substring-before to solve this problem. I wrote a sample xsl that I believe solves your problem, and I'll do my best to give you a clear description. When the named template "format-date" is first called, only one of the four parameters (date) is passed. The template first checks to see if a day has been passed as a parameter (<xsl:when test="$day=''">). If no day parameter is present, the template does a little processing to get the day out of the 'date' parameter - i.e the value before the first "/". It also truncates the date parameter to everything after the first "/", and then re-calls the same template with these two parameters. This same process happens for the month and the year - the template checks to see if they've been submitted as parameters, and if not, re-calls the template with a processed value for this parameter. The final when branch checks to see if all parameters are present, and if so, prints the results to the screen. HTH <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output encoding="UTF-8" omit-xml-declaration="yes" /> <xsl:template match="*"> <xsl:call-template name="format-date"> <xsl:with-param name="date">24/05/2002 22:55:10</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template name="format-date"> <xsl:param name="date" /> <xsl:param name="year" /> <xsl:param name="month" /> <xsl:param name="day" /> <xsl:choose> <xsl:when test="$day=''"> <xsl:call-template name="format-date"> <xsl:with-param name="date"> <xsl:value-of select="substring-after($date, '/')" /> </xsl:with-param> <xsl:with-param name="day"> <xsl:value-of select="substring-before($date, '/')" /> </xsl:with-param> </xsl:call-template> </xsl:when> <xsl:when test="$month=''"> <xsl:call-template name="format-date"> <xsl:with-param name="date"> <xsl:value-of select="substring-after($date, '/')" /> </xsl:with-param> <xsl:with-param name="day"> <xsl:value-of select="$day" /> </xsl:with-param> <xsl:with-param name="month"> <xsl:value-of select="substring-before($date, '/')" /> </xsl:with-param> </xsl:call-template> </xsl:when> <xsl:when test="$year=''"> <xsl:call-template name="format-date"> <xsl:with-param name="date"> <xsl:value-of select="substring-after($date, '/')" /> </xsl:with-param> <xsl:with-param name="day"> <xsl:value-of select="$day" /> </xsl:with-param> <xsl:with-param name="month"> <xsl:value-of select="$month" /> </xsl:with-param> <xsl:with-param name="year"> <xsl:value-of select="substring-before($date, ' ')" /> </xsl:with-param> </xsl:call-template> </xsl:when> <xsl:when test="$year!='' and $month!='' and $day!=''"> <xsl:value-of select="$day" /> / <xsl:value-of select="$month" /> / <xsl:value-of select="$year" /> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet> -----Original Message----- From: owner-hwg-languages(at)hwg.org [mailto:owner-hwg-languages(at)hwg.org]On Behalf Of Rob Sent: Tuesday, August 13, 2002 8:49 AM To: hwg-languages(at)hwg.org Subject: XSL Hi all, I'm wondering if someone could guide me in a problem I've come across in XSL. I have a field of type date but the actual data it contains is a combination of date+time (e.g. 24//05/2002 22:55:10). What I want to do is to take this format and display only the date part (i.e. strip out the time). Is there a way to do this using just XSL? I know that the date part is always in the format of ##/##/## so I'm figuring that doing a pattern match would be the way to go but if there's an element that will do this automatically that would be great. If someone could be so kind as to give me a pointer or two and/or to direct me to a website that might provide the info I'm looking for I would be most grateful. Cheers, Rob.
HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA