Marty Trenouth
Marty Trenouth

Reputation: 3752

Formatted Date in Attribute using XSLT

I'm looking at adding a title tag to a element with a formated date from an attribute. The input value is DateTime UTC format and i needes to be output in "pretty format".

I have a template that transforms the value. However I can't figure out how to call this template when puting the value into an attribute.

  <xsl:template name="formatDate">
    <xsl:param name="dateTime" />
    <xsl:variable name="date" select="substring-before($dateTime, 'T')" />
    <xsl:variable name="year" select="substring-before($date, '-')" />
    <xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
    <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
    <xsl:value-of select="concat($month, '-', $day, '-', $year)" />
  </xsl:template>

Upvotes: 2

Views: 1068

Answers (3)

Daniel Haley
Daniel Haley

Reputation: 52858

If you are using XSLT 2.0, you can skip calling a template all together and use format-dateTime():

<foo title="{format-dateTime(@lastReported,'[M]-[D]-[Y]')}"/>

Upvotes: 1

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

If you intend to re-use the same value, you could also do:

<xsl:variable name="title">
  <xsl:call-template name="formatDate">
    <xsl:with-param name="dateTime" select="@lastReported" />
  </xsl:call-template>
</xsl:variable>

<dummy titile="{$title}"/>

Upvotes: 1

Marty Trenouth
Marty Trenouth

Reputation: 3752

<xsl:attribute name="title">
  <xsl:call-template name="formatDate">
    <xsl:with-param name="dateTime" select="@lastReported" />
  </xsl:call-template>
</xsl:attribute>

Upvotes: 1

Related Questions