开发者

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>


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


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}"/>


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]')}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜