RE: [xsl] date formatting function

Subject: RE: [xsl] date formatting function
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 21 Sep 2004 09:48:02 +0100
Having set you off down this road, I don't particularly want to abandon you,
but it's not clear to me where you are stuck. You seem to be asking how to
divide the code you need to write between two templates/functions, when both
options you present are perfectly viable.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Bruce D'Arcus [mailto:bdarcus@xxxxxxxxxxxxx] 
> Sent: 21 September 2004 04:09
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] date formatting function
> 
> I'm about to integrate the configuration code that Wendell helped me 
> out with last week, but I'd like to work out one detail 
> related to date 
> formatting before doing so.
> 
> My configuration file currently has code like this for 
> specifying date 
> formatting:
> 
> 	<date>
> 	  <year>
> 	    <prefix>(</prefix>
> 	    <suffix>)</suffix>
> 	  </year>
> 	</date>
> 
> So here I would end up with (YYYY).
> 
> I'm still struggling with how to specify rendering for MM-DD 
> or MM, and 
> I think I'd like to make the decision based on the XSLT code 
> that would 
> actually render the dates.  So, I'd like to go back to this 
> suggestion 
> from Mike Kay on how to handle dates that could either be YYYY, 
> YYYY-MM, or YYYY-MM-DD.
> 
> > If you do want to use the gYearMonth data type, you can 
> write your own
> > function f:format-gYearMonth which converts the value to a date:
> >
> > xs:date(concat($gYM, '-01'))
> >
> > and then applies format-date() to it.
> 
> Basically, what I want to be able to do is to specify a config option 
> something like:
> 
> <date>
>    <month-day/>
> </date>
> 
> This would apply, for example, to the issue date in a 
> periodical, where 
> a complete citation -- with both year at the beginning and 
> month later 
> are both drawn from the same mods:dateIssued content -- would look 
> like:
> 
> 	Doe, J. (2000) Some Title, Some Periodical, v2, n3, 
> January, pp. 23-34.
> 
> .... or:
> 
> 	Smith, S. (1999) Some Other Title, Different 
> Periodical, v2, n3, March 
> 15, pp. 23-34.
> 
> So I want the function to do the proper thing whether the date is in 
> the form YYYY, YYYY-MM, or YYYY-MM-DD, where in the first 
> case, nothing 
> would be rendered, in the second I'd get only the month, and 
> the third 
> the month and day.  I also want there to be a parameter that 
> specifies 
> either the full month or abbreviated.
> 
> The function would then get invoked, I assume, within the 
> template that 
> maps the config file to the source data something vaguely like:
> 
> <xsl:template match="cs:month-day">
>    <xsl:param name="source"/>
>    <xsl:apply-templates select="bib:format-month-day{mods:mods)">
>      <xsl:with-param name="prefix" select="cs:prefix"/>
>      <xsl:with-param name="suffix" select="cs:suffix"/>
>    </xsl:apply-templates>
> </xsl:template>
> 
> The date in this case can be either of these:
>   	mods:relatedItem/mods:originInfo/mods:dateIssued
> 	mods:relatedItem/mods:part/mods:date
> 
> I understand conceptually I need to have a choose option in 
> which I am 
> testing if the string is castable as xs:gYearMonth or 
> xs:date, and that 
> if the former I need to concat the -01 to it but to ignore the day in 
> formatting, but I'm not really clear how I put it all together.
> 
> Here's where I'm at now:
> 
> <xsl:function name="bib:format-month-day" as="xs:integer">
>    <xsl:param name="mods" as="element(mods:mods)" />
>    <xsl:variable name="date" as="element(mods:dateIssued)"
>      select="$mods/mods:originInfo/mods:dateIssued" />
> <!-- need above to work for mods:part/mods:date also -->
>    <xsl:choose>
>      <xsl:when test="$date castable as xs:date">
>        ...
>      </xsl:when>
>      <xsl:when test="$date castable as xs:gYearMonth">
>        ...
>      </xsl:when>
>     <xsl:when test="$date castable as xs:gYear">
>        ...
>      </xsl:when>
>    </xsl:choose>
> </xsl:function>
> 
> Or is this the wrong approach, and I need to do the testing and 
> formatting from within the cs:month-day template, with this function 
> only creating the proper date format?
> 
> Bruce

Current Thread