Reputation: 599
I'm using XSLT 1.0 and need to convert a date format from dd/mm/yyyy to Month yyyy, e.g. January 2011.
Could anyone provide a sample for this? I can find examples for XSLT 2.0 but I'm using 1.0.
Thanks, Colin.
Upvotes: 3
Views: 6952
Reputation: 52878
I like Kirill's answer better, but here's one that doesn't use document()
:
XML Input
<doc>
<date>01/01/2012</date>
<date>01/02/2011</date>
<date>01/03/2010</date>
<date>01/04/2009</date>
</doc>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="date">
<xsl:variable name="month">
<xsl:call-template name="getMonth">
<xsl:with-param name="monthNbr" select="number(substring-before(substring-after(.,'/'),'/'))"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="year">
<xsl:call-template name="getYear">
<xsl:with-param name="date" select="."/>
</xsl:call-template>
</xsl:variable>
<date><xsl:value-of select="concat($month,' ',$year)"/></date>
</xsl:template>
<xsl:template name="getMonth">
<xsl:param name="monthNbr"/>
<xsl:choose>
<xsl:when test="$monthNbr=1">January</xsl:when>
<xsl:when test="$monthNbr=2">February</xsl:when>
<xsl:when test="$monthNbr=3">March</xsl:when>
<xsl:when test="$monthNbr=4">April</xsl:when>
<xsl:when test="$monthNbr=5">May</xsl:when>
<xsl:when test="$monthNbr=6">June</xsl:when>
<xsl:when test="$monthNbr=7">July</xsl:when>
<xsl:when test="$monthNbr=8">August</xsl:when>
<xsl:when test="$monthNbr=9">September</xsl:when>
<xsl:when test="$monthNbr=10">October</xsl:when>
<xsl:when test="$monthNbr=11">November</xsl:when>
<xsl:when test="$monthNbr=12">December</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="getYear">
<xsl:param name="date"/>
<xsl:variable name="year" select="substring-after($date,'/')"/>
<xsl:choose>
<xsl:when test="contains($year,'/')">
<xsl:call-template name="getYear">
<xsl:with-param name="date" select="$year"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$year"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XML Output
<doc>
<date>January 2012</date>
<date>February 2011</date>
<date>March 2010</date>
<date>April 2009</date>
</doc>
Also, I used the template for the year instead of substring()
to handle months/days that don't have leading zeros.
Upvotes: 4
Reputation: 12154
obviously its not possible with XSLT 1.0 alone .. for a similar Q I suggested scripting .. (C#) ..
Sample XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<date>02/10/2012</date>
<date>2/9/2012</date>
</root>
Sample XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:cs="urn:cs">
<xsl:output method="xml" indent="yes"/>
<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
private static string[] formats = new string[]
{
"dd/MM/yyyy",
"dd/M/yyyy",
"d/M/yyyy",
"d/MM/yyyy",
"dd/MM/yy",
"dd/M/yy",
"d/M/yy",
"d/MM/yy"
};
public string date_conv(string date1)
{
DateTime dDateTime;
DateTime.TryParseExact(date1, formats, new global::System.Globalization.CultureInfo("en-US"), global::System.Globalization.DateTimeStyles.None, out dDateTime);
return(String.Format("{0:MMMM yyyy}", dDateTime));
}
]]>
</msxsl:script>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="cs:date_conv(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Resulting Output:
<?xml version="1.0" encoding="utf-8"?>
<root>
<date>October 2012</date>
<date>September 2012</date>
</root>
The script may reside in a same file (like I have it in my sample XSLT code) or if the code triggering XSLTransformation is C# then move the same code in the calling place :)
Upvotes: 0
Reputation: 56182
Use:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="metadata">
<xsl:output method="text"/>
<ext:months>
<month>January</month>
<month>February</month>
<month>March</month>
<month>April</month>
<month>May</month>
<month>June</month>
<month>July</month>
<month>August</month>
<month>September</month>
<month>October</month>
<month>November</month>
<month>December</month>
</ext:months>
<xsl:variable name="date">23/02/2012</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="concat(document('')
//month[number(substring($date, 4, 2))],
' ',
substring($date, 7, 4))"/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 4