使用 XSLT 将日期时间转换为完整的单词
Use XSLT to convert date time to full words
我刚开始学习 XSLT,希望能转换一些 XML 时间和日期数据,以便完整拼写。虽然我看到了将日期信息输出为单词(12 到 12 月)的方法,但我看不到任何可以转换时间(6:30 到 6 点 30 分)或年份(2005 到 2005 年)的方法。
是否有一些标准的方法可以做到这一点?如果不是,使用 xsl:if 这样做是否可行(如果冗长)?我可能可以将输入限制在刻钟内。
那一年呢?
有人可能写了一些有用的东西,并且会 post 在这里 link,但与此同时,一个快速而肮脏的解决方案可能是:
<xsl:choose>
<xsl:when test='hour = 1'>
one
</xsl:when>
<xsl:when test='hour = 2>
two
</xsl:when>
...
</xsl:choose>
Date/time提取函数在这里:
http://www.w3schools.com/xpath/xpath_functions.asp#datetime
在 XSLT 2.0 中,您可以使用 the format-date(), format-time() and format-dateTime() functions 以多种方式格式化日期和时间,例如:
XML
<input>2014-04-30T15:45:12</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:value-of select="format-dateTime(input,
'The [DWwo] Day of [MNn] in the Year [YWw], at [hWw] Hours and [mWv] Minutes [PN]'
)" />
</output>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>The Thirtieth Day of April in the Year Two Thousand and Fourteen, at Three Hours and Forty Five Minutes P.M.</output>
我刚开始学习 XSLT,希望能转换一些 XML 时间和日期数据,以便完整拼写。虽然我看到了将日期信息输出为单词(12 到 12 月)的方法,但我看不到任何可以转换时间(6:30 到 6 点 30 分)或年份(2005 到 2005 年)的方法。
是否有一些标准的方法可以做到这一点?如果不是,使用 xsl:if 这样做是否可行(如果冗长)?我可能可以将输入限制在刻钟内。
那一年呢?
有人可能写了一些有用的东西,并且会 post 在这里 link,但与此同时,一个快速而肮脏的解决方案可能是:
<xsl:choose>
<xsl:when test='hour = 1'>
one
</xsl:when>
<xsl:when test='hour = 2>
two
</xsl:when>
...
</xsl:choose>
Date/time提取函数在这里: http://www.w3schools.com/xpath/xpath_functions.asp#datetime
在 XSLT 2.0 中,您可以使用 the format-date(), format-time() and format-dateTime() functions 以多种方式格式化日期和时间,例如:
XML
<input>2014-04-30T15:45:12</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:value-of select="format-dateTime(input,
'The [DWwo] Day of [MNn] in the Year [YWw], at [hWw] Hours and [mWv] Minutes [PN]'
)" />
</output>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>The Thirtieth Day of April in the Year Two Thousand and Fourteen, at Three Hours and Forty Five Minutes P.M.</output>