格式化并在xslt中显示日期时间

Nao*_*aor 13 xml xslt xpath

我有一个带有日期时间数据的XML:

<A>
    <StartDate>2011-11-01T00:00:00</StartDate>
    <EndDate>2011-11-30T00:00:00</EndDate>
    <IsRecurring>false</IsRecurring>
</A>
Run Code Online (Sandbox Code Playgroud)

我需要在xslt中只使用以下格式的日期:

01/11/2011 - 30/11/2011
Run Code Online (Sandbox Code Playgroud)

当我做:

<xsl:value-of select="A/StartDate/"> - <xsl:value-of select="A/EndDate/">
Run Code Online (Sandbox Code Playgroud)

我明白了:

2011-11-01T00:00:00 - 2011-11-30T00:00:00
Run Code Online (Sandbox Code Playgroud)

如何正确显示?

Kir*_*huk 19

看看XPath字符串函数:substring,substring-before等.

参考:http://www.w3.org/TR/xpath/#section-String-Functions

<xsl:variable name="dt" select="'2011-11-01T12:13:59'"/>

        <xsl:value-of select="concat(
                      substring($dt, 9, 2),
                      '/',
                      substring($dt, 6, 2),
                      '/',
                      substring($dt, 1, 4)
                      )"/>
Run Code Online (Sandbox Code Playgroud)


JBe*_*ert 13

编辑:如果您仍然需要使用XSLT 1.x,您可以查看EXSLT的日期:format-date用户函数.在那种情况下,模式是dd/MM/yyyy.

如果您使用的是XSLT 2.0,则使用该format-dateTime功能更为方便.

这是您在XSLT 2.0中的示例:

<xsl:value-of select="format-dateTime(A/StartDate, '[D01]/[M01]/[Y0001]')" /> 
- <xsl:value-of select="format-dateTime(A/EndDate, '[D01]/[M01]/[Y0001]')" />
Run Code Online (Sandbox Code Playgroud)