问题
I'm trying to format a certain date format in xslt. This is the format:
2012-7-19
I'd like to convert this to a numerical value in xslt for sorting, so that the above would become:
20120719
The problem is that single months/days don't have a 0 in front of them. So I need to somehow add that in front of single digit months/days, but not in front of months/days that have two digits. Does anyone know how I can do this?
I have this so far:
<xsl:value-of select="concat(
substring(substring-after(substring-after(./AirDate, '/'),'/'),0,5),
substring(substring-after(./AirDate, '/'), 0, 3),
substring-before(./AirDate, '/'))"/>
but that occasionally throws in a /
for single digit days, and doesn't put a 0 in front of single digit months/days
I don't have the ability to change the data source before passing it to the xslt, and I have to use xslt version 1.0.
回答1:
I think format-number will do the trick.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="date" select="'2007-3-19'" />
<xsl:template name="format_date" >
<xsl:param name ="date" />
<xsl:variable name ="year" select="substring-before($date, '-')" />
<xsl:variable name ="month_and_day" select="substring-after($date, '-')" />
<xsl:variable name ="day" select="substring-after($month_and_day, '-')" />
<xsl:variable name ="month" select="substring-before($month_and_day, '-')" />
<xsl:value-of select="$year"/>
<xsl:value-of select="format-number($month, '00')"/>
<xsl:value-of select="format-number($day, '00')"/>
</xsl:template>
<xsl:template match="/" >
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
回答2:
I'm sure there's a more elegant way of doing this even with XSLT 1.0, but this rather brutish solution is one option:
Stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="utf-8"/>
<xsl:variable name="HYPHEN" select="'-'"/>
<xsl:template name="zero-pad">
<xsl:param name="number"/>
<xsl:choose>
<xsl:when test="string-length($number) = 1">
<xsl:value-of select="concat('0', $number)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$number"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="format-date">
<xsl:param name="date"/>
<xsl:variable name="year" select="substring-before($date, $HYPHEN)"/>
<xsl:variable name="month-and-day"
select="substring-after($date, concat($year, $HYPHEN))"/>
<xsl:variable name="month">
<xsl:call-template name="zero-pad">
<xsl:with-param name="number"
select="substring-before($month-and-day, $HYPHEN)"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="day">
<xsl:call-template name="zero-pad">
<xsl:with-param name="number"
select="substring-after($month-and-day, $HYPHEN)"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="number(concat($year, $month, $day))"/>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="format-date">
<xsl:with-param name="date" select="'2012-7-19'"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Output
20120719
回答3:
You can use a combination of substring-after
and substring-before
to extract the pieces, and then handle them as numbers instead than strings - i.e. uses multiplications and additions to generate the final value.
Something like this:
<xsl:variable name="sep" select="'-'"/>
<xsl:variable name="date" select="."/>
<xsl:value-of select="substring-after(substring-after($date, $sep),$sep) + substring-before(substring-after($date, $sep),$sep)*100 + substring-before($date, $sep)*10000"/>
setting the sep
and date
variables to the separator to use and the original date string respectively (in your question the sample uses -
as the separator, but the XSLT uses /
)
回答4:
Try this:
<xsl:value-of select="concat(substring(date, 7, 4), '-', substring(date, 4, 2), '-', substring(date, 1, 2))"/>
来源:https://stackoverflow.com/questions/16110848/formatting-date-in-xslt