3

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.

1

3 Answers 3

4

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>
2
  • Thanks for the response. I'm now getting the error: Execution of the 'document()' function was prohibited. Use the XsltSettings.EnableDocumentFunction property to enable it I can't modify the way the xslt is read or written, how can I workaround the document('') declaration? Feb 23, 2012 at 16:08
  • @ColinBrown, If you can't modify the way XSLT processor is executed, you can use <xsl:choose> like @DevNull suggested. Feb 23, 2012 at 17:20
4

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.

7
  • Thanks for this, would you mind explaining how I can call the function from within an existing xslt file? I've given the template a name "date" and trying this but it's not working: <xsl:call-template name="date"> <xsl:with-param name="string" select="p:StartDate"/> </xsl:call-template> Feb 23, 2012 at 21:35
  • @ColinBrown - What does "not working" mean? Are you getting an error? Are you getting any output? Is p:StartDate a direct child of the context you're doing the call-template from? Feb 24, 2012 at 0:32
  • Also, I have two named templates in my solution; which template did you add to your XSLT? Feb 24, 2012 at 0:37
  • Hi, by not working I mean it returns blank, no error. I gave the following line the name "date" <xsl:template match="date"> - I think that's the right one? Feb 24, 2012 at 10:15
  • I'm assuming you changed <xsl:template match="date"> to <xsl:template name="date"> and that you added <xsl:param name="string"> so you can pass it the value of the actual date. If that is true then the issue is most likely that you're not getting a value from <xsl:with-param name="string" select="p:StartDate"/>. You can try adding an xsl:message to your template to see what's getting passed. Something like <xsl:message>Value of param "string" is "<xsl:value-of select="$string"/>".</xsl:message>. Feb 24, 2012 at 15:22
0

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 :)

2
  • 1
    "obviously its not possible with XSLT 1.0 alone"? Feb 23, 2012 at 16:56
  • @DevNull, :) well I assumed date to be in the above mentioned formats .. this code uses tryparse method which can digest and convert it to desired format .. where as your method can only read "dd/MM/yyyy" .. well I take my words back! not possible isn't a good choice of word :) Feb 24, 2012 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.