我是xslt的新手。我一直在努力使用dateTime格式。我需要的是 从12/2/2011 2:57:18 AM转换为12022011T02:57:18
你怎么能这样做?我真的很感谢你的帮助。请一个好例子!谢谢你的时间:))
答案 0 :(得分:0)
这样做:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:call-template name="FormatDateTime">
<xsl:with-param name="dateTime" select="'12/2/2011 2:57:18 AM'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="FormatDateTime">
<xsl:param name="dateTime" />
<xsl:call-template name="FormatDate">
<xsl:with-param name="date" select="substring-before($dateTime, ' ')" />
</xsl:call-template>
<xsl:text>T</xsl:text>
<xsl:call-template name="FormatTime">
<xsl:with-param name="time" select="substring-after($dateTime, ' ')" />
</xsl:call-template>
</xsl:template>
<xsl:template name="FormatTime">
<xsl:param name="time" />
<xsl:variable name="timeOnly" select="substring-before($time, ' ')" />
<xsl:variable name="amPm" select="substring-after($time, ' ')" />
<xsl:variable name="hour" select="substring-before($time, ':')" />
<xsl:variable name="hourAdjusted" select="$hour mod 12 + 12 * ($amPm = 'PM')" />
<xsl:variable name="afterHour" select="substring-after($timeOnly, ':')" />
<xsl:variable name="minutes" select="substring-before($afterHour, ':')" />
<xsl:variable name="seconds" select="substring-after($afterHour, ':')" />
<xsl:value-of select="concat(format-number($hourAdjusted, '00'), ':',
format-number($minutes, '00'), ':',
format-number($seconds, '00'))"/>
</xsl:template>
<xsl:template name="FormatDate">
<xsl:param name="date" />
<xsl:variable name="month" select="substring-before($date, '/')" />
<xsl:variable name="afterMonth" select="substring-after($date, '/')" />
<xsl:variable name="day" select="substring-before($afterMonth, '/')" />
<xsl:variable name="year" select="substring-after($afterMonth, '/')" />
<xsl:value-of select="concat(format-number($month, '00'),
format-number($day, '00'),
$year)"/>
</xsl:template>
</xsl:stylesheet>
此XSLT中包含一个示例值,因此在运行时,它将生成:
12022011T02:57:18
但是你确定你希望结果格式是MMddyyyy,而不是yyyy-MM-dd吗?如果您真的想要后者,可以更改此部分:
<xsl:value-of select="concat(format-number($month, '00'),
format-number($day, '00'),
$year)"/>
到此:
<xsl:value-of select="concat($year, '-',
format-number($month, '00'), '-'
format-number($day, '00'))"/>
答案 1 :(得分:0)
xml示例
<?xml version="1.0" encoding="utf-8"?>
<root>
<mydate atribdate="12/05/2010 22:10:10">01/10/2012 12:33:00</mydate>
<mydate atribdate="">12/12/2012 12:33:00</mydate>
</root>
xsl sample xslt 1.0
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match = "/">
<xsl:apply-templates select="root/mydate" />
</xsl:template>
<xsl:template match="mydate">
date element---><xsl:value-of select="translate(translate(.,'/',''),' ','T')"/>
date attirbute---><xsl:value-of select="translate(translate(@atribdate,'/',''),' ','T')"/>
</xsl:template>
</xsl:stylesheet>
输出xml
<?xml version="1.0" encoding="UTF-8"?>
date element--->01102012T12:33:00
date attirbute--->12052010T22:10:10
date element--->12122012T12:33:00
date attirbute--->