Re: [xsl] date formatting function

Subject: Re: [xsl] date formatting function
From: Bruce D'Arcus <bdarcus@xxxxxxxxxxxxx>
Date: Sat, 25 Sep 2004 14:32:04 -0400
followup:

On Sep 21, 2004, at 8:00 AM, Bruce D'Arcus wrote:

I guess my question, given what I want to do, is how should I construct the function; as a single function -- let's call it bib:format-month-day -- that when called takes any YYYY/YYYY-MM/YYYY-MM-DD string and returns the formatted date (with parameter for whether months are rendered as full or abbreviated)?

That's where I was headed. But if that's sensible, I'm not sure how to actually code it; .e.g. what goes inside the "when" below:

     <xsl:when test="$date castable as xs:gYearMonth">
       ...
     </xsl:when>

Or, more specifically, how do I take the statement "xs:date(concat($date, '-01'))" and then use format-date on it?

Here's what I've done:


  <xsl:function name="mods:format-month-day">
    <xsl:param name="date"/>
    <xsl:variable name="date" select="$date" />
    <xsl:choose>
      <xsl:when test="$date castable as xs:gYearMonth">
        <xsl:variable name="month-day"
          select="xs:date(concat($date, '-01'))"/>
        <xsl:value-of
          select="format-date($month-day,'[MNn,*-3].')"/>
      </xsl:when>
      <xsl:when test="$date castable as xs:date">
        <xsl:value-of select="format-date($date,'[MNn,*-3]. [D]')"/>
      </xsl:when>
    </xsl:choose>
  </xsl:function>

  <xsl:template match="date">
    <h2>Dates:</h2>
    <p class="date">
      <xsl:value-of select="mods:format-month-day(.)"/>
    </p>
   </xsl:template>

Now, the bigger question: I need to later internationalize date formatting. To that end, , where the root of the config file I am using to configure the formatting will hold an xml:lang attribute ("cs:citationstyle/@lang") .

So:

a) how to do this with the above approach?

b) what are the advantages/disadvantages of the format-date approach above, versus the below (which is what I currently have)? In theory, I guess the format-date approach is only useful if it already handles all of the internationalization of month formatting I'm doing manually below. In either case, I need to change things to make more configurable with respect to order and punctuation (9 September 2000 vs. September 9, 2000, etc.).

<xsl:template match="mods:date | mods:dateIssued | mods:url/@dateLastAccessed">
<xsl:variable name="month-part">
<xsl:choose>
<xsl:when test="substring(.,6,2) = '01'">January</xsl:when>
<xsl:when test="substring(.,6,2) = '02'">February</xsl:when>
<xsl:when test="substring(.,6,2) = '03'">March</xsl:when>
<xsl:when test="substring(.,6,2) = '04'">April</xsl:when>
<xsl:when test="substring(.,6,2) = '05'">May</xsl:when>
<xsl:when test="substring(.,6,2) = '06'">June</xsl:when>
<xsl:when test="substring(.,6,2) = '07'">July</xsl:when>
<xsl:when test="substring(.,6,2) = '08'">August</xsl:when>
<xsl:when test="substring(.,6,2) = '09'">September</xsl:when>
<xsl:when test="substring(.,6,2) = '10'">October</xsl:when>
<xsl:when test="substring(.,6,2) = '11'">November</xsl:when>
<xsl:when test="substring(.,6,2) = '12'">December</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="month-part-abbrev">
<xsl:choose>
<xsl:when test="substring(.,6,2) = '01'">Jan</xsl:when>
<xsl:when test="substring(.,6,2) = '02'">Feb</xsl:when>
<xsl:when test="substring(.,6,2) = '03'">Mar</xsl:when>
<xsl:when test="substring(.,6,2) = '04'">Apr</xsl:when>
<xsl:when test="substring(.,6,2) = '05'">May</xsl:when>
<xsl:when test="substring(.,6,2) = '06'">Jun</xsl:when>
<xsl:when test="substring(.,6,2) = '07'">Jul</xsl:when>
<xsl:when test="substring(.,6,2) = '08'">Aug</xsl:when>
<xsl:when test="substring(.,6,2) = '09'">Sept</xsl:when>
<xsl:when test="substring(.,6,2) = '10'">Oct</xsl:when>
<xsl:when test="substring(.,6,2) = '11'">Nov</xsl:when>
<xsl:when test="substring(.,6,2) = '12'">Dec</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="day-part">
<xsl:choose>
<xsl:when test="substring(.,9,1) = '0'">
<xsl:value-of select="substring(.,10,1)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(.,9,2)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="substring(.,6,2)">
<xsl:text> </xsl:text>
<xsl:value-of select="$month-part"/>
</xsl:if>
<xsl:if test="substring(.,9,2)">
<xsl:text> </xsl:text>
<xsl:value-of select="$day-part"/>
</xsl:if>
<xsl:if test="substring(.,6,2)">
<xsl:text></xsl:text>
</xsl:if>
<xsl:if test="../@dateLastAccessed">
<xsl:text>, </xsl:text>
<xsl:value-of select="substring(.,1,4)"/>
</xsl:if>
</xsl:template>


Bruce

Current Thread