在XSLT中转换时间字符串
如何转换时间字符串,如在XSLT中转换时间字符串,xslt,Xslt,如何转换时间字符串,如 20101115083000 +0200 到 使用XSLT?我们使用模板: <xsl:template name="format-date-time"> <xsl:param name="date" select="'%Y-%m-%dT%H:%M:%S%z'"/> <xsl:value-of select="substring($date, 9, 2)"/> <xsl:text>/</xsl:
20101115083000 +0200
到
使用XSLT?我们使用模板:
<xsl:template name="format-date-time">
<xsl:param name="date" select="'%Y-%m-%dT%H:%M:%S%z'"/>
<xsl:value-of select="substring($date, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring($date, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring($date, 1, 4)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring($date, 12, 2)"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="substring($date, 15, 2)"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="substring($date, 18, 2)"/>
</xsl:template>
/
/
:
:
我们这样称呼这些模板:
<xsl:call-template name="format-date-time">
<xsl:with-param name="date" select="./Startdate"/>
</xsl:call-template>
/Startdate是一个XML日期,但是使用子字符串技术,我认为您也可以解决您的问题。如果您有XSLT 2.0,您可以使用 如果您有XSLT1.0,但可以使用EXSLT,那么它会提供
与@Peter的显式代码相比,这些代码使用起来不那么透明,但如果您的输入格式可以变化,可能会更加健壮。这里是一个最通用的formatDateTime处理。输入和输出格式完全可配置,并作为参数传递:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="convertDateTime">
<xsl:with-param name="pDateTime" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat">
<year offset="0" length="4"/>
<month offset="4" length="2"/>
<day offset="6" length="2"/>
<hour offset="8" length="2"/>
<minutes offset="10" length="2"/>
<seconds offset="12" length="2"/>
<zone offset="15" length="5"/>
</xsl:param>
<xsl:param name="pOutFormat">
<y/>-<mo/>-<d/> <h/>:<m/>:<s/> <z/>
</xsl:param>
<xsl:variable name="vInFormat" select=
"document('')/*/xsl:template[@name='convertDateTime']
/xsl:param[@name='pInFormat']"/>
<xsl:variable name="vOutFormat" select=
"document('')/*/xsl:template[@name='convertDateTime']
/xsl:param[@name='pOutFormat']"/>
<xsl:apply-templates select="$vOutFormat/node()"
mode="_convertDateTime">
<xsl:with-param name="pDateTime" select="$pDateTime"/>
<xsl:with-param name="pInFormat" select="$vInFormat"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="y" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/year/@offset,
$pInFormat/year/@length)"/>
</xsl:template>
<xsl:template match="mo" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/month/@offset,
$pInFormat/month/@length)"/>
</xsl:template>
<xsl:template match="d" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/day/@offset,
$pInFormat/day/@length)"/>
</xsl:template>
<xsl:template match="h" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/hour/@offset,
$pInFormat/hour/@length)"/>
</xsl:template>
<xsl:template match="m" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/minutes/@offset,
$pInFormat/minutes/@length)"/>
</xsl:template>
<xsl:template match="s" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/seconds/@offset,
$pInFormat/seconds/@length)"/>
</xsl:template>
<xsl:template match="z" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/zone/@offset,
$pInFormat/zone/@length)"/>
</xsl:template>
</xsl:stylesheet>
谢谢,但由于我对XSL非常陌生,我更喜欢一个更简单的解决方案;)@blugray:除了每次输入格式和所需输出格式发生更改时,您都必须一次又一次地编写新的xslt代码并进行调试。我在这里发布的只是针对所有此类情况的单一解决方案——永远不要再编写任何xslt代码来格式化日期时间。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="convertDateTime">
<xsl:with-param name="pDateTime" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat">
<year offset="0" length="4"/>
<month offset="4" length="2"/>
<day offset="6" length="2"/>
<hour offset="8" length="2"/>
<minutes offset="10" length="2"/>
<seconds offset="12" length="2"/>
<zone offset="15" length="5"/>
</xsl:param>
<xsl:param name="pOutFormat">
<y/>-<mo/>-<d/> <h/>:<m/>:<s/> <z/>
</xsl:param>
<xsl:variable name="vInFormat" select=
"document('')/*/xsl:template[@name='convertDateTime']
/xsl:param[@name='pInFormat']"/>
<xsl:variable name="vOutFormat" select=
"document('')/*/xsl:template[@name='convertDateTime']
/xsl:param[@name='pOutFormat']"/>
<xsl:apply-templates select="$vOutFormat/node()"
mode="_convertDateTime">
<xsl:with-param name="pDateTime" select="$pDateTime"/>
<xsl:with-param name="pInFormat" select="$vInFormat"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="y" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/year/@offset,
$pInFormat/year/@length)"/>
</xsl:template>
<xsl:template match="mo" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/month/@offset,
$pInFormat/month/@length)"/>
</xsl:template>
<xsl:template match="d" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/day/@offset,
$pInFormat/day/@length)"/>
</xsl:template>
<xsl:template match="h" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/hour/@offset,
$pInFormat/hour/@length)"/>
</xsl:template>
<xsl:template match="m" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/minutes/@offset,
$pInFormat/minutes/@length)"/>
</xsl:template>
<xsl:template match="s" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/seconds/@offset,
$pInFormat/seconds/@length)"/>
</xsl:template>
<xsl:template match="z" mode="_convertDateTime">
<xsl:param name="pDateTime"/>
<xsl:param name="pInFormat"/>
<xsl:value-of select=
"substring($pDateTime,
1+$pInFormat/zone/@offset,
$pInFormat/zone/@length)"/>
</xsl:template>
</xsl:stylesheet>
<dateTime>20101115083000 +0200</dateTime>
2010-11-15 08:30:00 +0200