XSLT:获取月份缩写

时间:2013-02-12 00:27:42

标签: xslt xslt-1.0

我有一个表示日期的字符串,格式如此

2010-12-03 =" yyyy-mm-dd"

我想使用XSLT并找到一种方法来提取月份并获得月份缩写。

转换后应该读取" DEC "自12月是第12个月。

谢谢!

2 个答案:

答案 0 :(得分:6)

您可以将其添加到XSLT中:

<xsl:variable name="months" select="'  JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'" />

然后,只要您需要月份缩写,请使用:

substring($months, substring(date, 6, 2) * 3, 3)

其中date将是对您的节点的引用,其中包含日期。

答案 1 :(得分:3)

这是一个简短的演示,如何智能和有效地执行此操作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:param name="pMonthNames">
  <name><short>JAN</short>January</name>
  <name><short>FEB</short>February</name>
  <name><short>MAR</short>March</name>
  <name><short>APR</short>April</name>
  <name><short>MAY</short>May</name>
  <name><short>JUN</short>June</name>
  <name><short>JUL</short>July</name>
  <name><short>AUG</short>August</name>
  <name><short>SEP</short>September</name>
  <name><short>OCT</short>October</name>
  <name><short>NOV</short>November</name>
  <name><short>DEC</short>December</name>
 </xsl:param>

 <xsl:variable name="vMonthNames" select=
     "document('')/*/xsl:param[@name='pMonthNames']/*"/>

 <xsl:template match="t">
     <xsl:value-of select="$vMonthNames[position()=substring(current(),6,2)]/short"/>
 </xsl:template>
</xsl:stylesheet>

对以下XML文档应用此转换时:

<t>2010-12-03</t>

产生了想要的正确结果:

DEC
相关问题