Reputation: 1281
What's the options to show/output date in HTML in some sort of readable format. I mean if I simply show value of date_last_activity it looks wierd :S So my question IS does XSLT offers any date format conversions at all?
XML:
<date_last_activity format="MMDDCCYY">04132012</date_last_activity>
XLS template:
<xsl:value-of select="date_last_activity"/>
HTML:
04132012
Would rather like it to look like 04/13/2012...
I readed a lot on XSLT one, I guess it's possible to make "template" => formatDate... but no idea where to start :S
Upvotes: 2
Views: 11632
Reputation: 3381
If you need to do it n more than one place, create a named template somewhere in your xsl
EDIT: template didn't appear because of wrong formatting...
<xsl:template name="formats_date">
<xsl:param name="raw_date" />
<xsl:value-of select="concat( substring( $raw_date, 0, 2 ), '/', substring( $raw_date, 2, 2 ), '/', substring( $raw_date, 4 ) )" />
</xsl:template>
Then apply it as
<xsl:call-template name="formats_date">
<xsl:with-param name="raw_date" select="date_last_activity" />
</xsl:call-template/>
Otherwise you can just do the xsl:value-of concat... stuff directly where you need it
Upvotes: 1
Reputation: 163322
XSLT 2.0 offers the function format-date(). However, before using it you will need to convert your date to ISO format, YYYY-MM-DD, which can be done using concat and substring.
If you're still stuck with XSLT 1.0, there's a library of date-handling functions and templates available at www.exslt.org.
Upvotes: 1
Reputation: 243449
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="date_last_activity">
<xsl:value-of select=
"concat(
substring(.,
string-length(substring-before(@format, 'MM'))+1,
2
),
'/',
substring(.,
string-length(substring-before(@format, 'DD'))+1,
2
),
'/',
substring(.,
string-length(substring-before(@format, 'CCYY'))+1,
4
)
)"/>
</xsl:template>
</xsl:stylesheet>
when applied to the provided XML document:
<date_last_activity format="MMDDCCYY">04132012</date_last_activity>
produces the wanted, correct result:
04/13/2012
When the same transformation is applied to the following XML document:
<date_last_activity format="CCYYDDMM">20121304</date_last_activity>
the same correct result is produced:
04/13/2012
Upvotes: 4