我们如何在 XSLT 中将日期格式从 DD-MMM-YYYY 转换为 YYYY-MM-DD。
2013 年 1 月 10 日至 20130110
在 XSLT 1.0 中
这对于一个元素来说非常简单xsl:choose
,并且不需要任何扩展。
这个样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="date">
<xsl:copy>
<xsl:call-template name="date">
<xsl:with-param name="dd-mmm-yyyy" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="date">
<xsl:param name="dd-mmm-yyyy"/>
<xsl:variable name="dd" select="substring-before($dd-mmm-yyyy, '-')"/>
<xsl:variable name="mmm-yyyy" select="substring-after($dd-mmm-yyyy, '-')"/>
<xsl:variable name="mmm" select="substring-before($mmm-yyyy, '-')"/>
<xsl:variable name="yyyy" select="substring-after($mmm-yyyy, '-')"/>
<xsl:value-of select="$yyyy"/>
<xsl:choose>
<xsl:when test="$mmm = 'JAN'">01</xsl:when>
<xsl:when test="$mmm = 'FEB'">02</xsl:when>
<xsl:when test="$mmm = 'MAR'">03</xsl:when>
<xsl:when test="$mmm = 'APR'">04</xsl:when>
<xsl:when test="$mmm = 'MAY'">05</xsl:when>
<xsl:when test="$mmm = 'JUN'">06</xsl:when>
<xsl:when test="$mmm = 'JUL'">07</xsl:when>
<xsl:when test="$mmm = 'AUG'">08</xsl:when>
<xsl:when test="$mmm = 'SEP'">09</xsl:when>
<xsl:when test="$mmm = 'OCT'">10</xsl:when>
<xsl:when test="$mmm = 'NOV'">11</xsl:when>
<xsl:when test="$mmm = 'DEC'">12</xsl:when>
</xsl:choose>
<xsl:value-of select="$dd"/>
</xsl:template>
</xsl:stylesheet>
应用于此XML数据
<?xml version="1.0" encoding="UTF-8"?>
<root>
<date>10-JAN-2013</date>
<date>04-JUL-1776</date>
<date>31-DEC-1999</date>
</root>
产生这个输出
<?xml version="1.0" encoding="utf-8"?>
<root>
<date>20130110</date>
<date>17760704</date>
<date>19991231</date>
</root>
如果你可以使用node-set作为 xslt 1.0 处理器的扩展,你可以试试这个。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="date" select="'10-JAN-2013'" />
<xsl:variable name="month_data_tmp">
<month short="JAN" nr="01" />
<!--- and so on for each month -->
</xsl:variable>
<xsl:variable name="month_data" select="exsl:node-set($month_data_tmp)" />
<xsl:template name="format_date" >
<xsl:param name ="date" />
<xsl:variable name ="day" select="substring-before($date, '-')" />
<xsl:variable name ="month_and_year" select="substring-after($date, '-')" />
<xsl:variable name ="year" select="substring-after($month_and_year, '-')" />
<xsl:variable name ="month" select="substring-before($month_and_year, '-')" />
<xsl:value-of select="$year"/>
<xsl:value-of select="$month_data/month[@short=$month]/@nr"/>
<xsl:value-of select="$day"/>
</xsl:template>
<xsl:template match="/" >
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</xsl:template>
输出将是:
20130110
更新命令中的附加问题:
您可以在您以前使用过的任何地方调用模板<xsl:value-of select="$date"/>
。
<result>
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</result>
或者您可以将结果分配给一个新变量并使用它。
<xsl:variable name="newdate">
<xsl:call-template name="format_date" >
<xsl:with-param name ="date" select="$date"/>
</xsl:call-template>
</xsl:variable>
<result>
<xsl:value-of select="$newdate"/>
</result>
我意识到这个帖子现在已经过时了,但我想我会分享我的模板,以防有人想通过复制和粘贴来节省一些时间。感谢上面的答案,因为这是基于它们。我的模板只是从 dd/MM/yyyy 转换为 yyyy-MM-dd;以及从 dd/MM/yyyy HH:mm:ss 到 yyyy-MM-ddTHH:mm:ss。
<!--
Template Name: Date
Description: Takes a date in the format dd/MM/yyyy and outputs it in the format yyyy-mm-dd
Additionally will take a datetime in the format dd/MM/yyyy HH:mm:ss and output in the format yyyy-MM-ddTHH:mm:ss
-->
<xsl:template name="date">
<xsl:param name="slashFormattedDate"/>
<xsl:param name="hasTime"/>
<xsl:variable name="dd" select="substring-before($slashFormattedDate, '/')"/>
<xsl:variable name="monthYear" select="substring-after($slashFormattedDate, '/')"/>
<xsl:variable name="mm" select="substring-before($monthYear, '/')"/>
<xsl:variable name="yyyyTemp" select="substring-after($monthYear, '/')"/>
<xsl:variable name="yyyy">
<xsl:choose>
<xsl:when test="$hasTime='Y'">
<xsl:value-of select="substring-before($yyyyTemp, ' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$yyyyTemp"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$hasTime='Y'">
<xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>T<xsl:value-of select="substring-after($yyyyTemp, ' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>