XSL:无法显示日期时间

时间:2014-01-03 17:45:05

标签: xml xslt xpath

我是XSL的新手,我尝试显示日期时间。

XML中的信息在属性中提供:     date =“2013-08-20 00:18:35”

我想在html页面中显示这种格式的日期:     2013年8月20日00:18(或2013年8月20日00:18)

暂时,我尝试了许多没有成功的事情......

    <xsl:variable name="dt" select="@date" /> //OK
    <xsl:variable name="dtime" select="translate($dt, ' ', 'T')" />  //OK 2013-08-20T00:18:35
    <xsl:variable name="datet" as="xs:dateTime" select="xs:dateTime($dtime)"/>
    <!--
    <xsl:variable name="datet" select="'2011-11-01T12:13:59'"/>
    -->
    <xsl:variable name="finaldate" select="format-dateTime($datet, '[Y0001]/[M01]/[D01]', (), (), ())"/>
    <xsl:value-of select="$finaldate" />
目前

2结果:

  • 没有价值
  • XSLT转换期间出错:调用了一个未知的XPath扩展函数。

有什么想法吗?

谢谢。


[编辑]

这里是XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="messages.xsl"?>
<AllMessages address="test">
    <messages flag="2" date="2013-08-20 00:18:35" rowid="19120">
        <text>blabla</text>
    </messages>
    <messages flag="3" date="2013-08-20 00:19:58" rowid="19121">
        <text>No pb \u00e7a arrive.</text>
    </messages>
</AllMessages>

和XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0">
    <!-- <xsl:output method = "html" version="5.0" encoding="UTF-8" indent = "yes" />-->

    <xsl:template match="/">
        <!-- <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html></xsl:text> -->
        <html>
            <head>
                <title>Messages</title>
                <!-- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> -->
                <link rel="stylesheet" type="text/css" href="messages.css" />
            </head>
            <body>
                <xsl:for-each select="AllMessages/messages">
                    <div id="date">
                        <xsl:variable name="dt" select="@date" />   
                        <xsl:variable name="dtime" select="translate($dt, ' ', 'T')" />                 


                        <xsl:variable name="datet" as="xs:dateTime" select="xs:dateTime($dtime)"/>

                        <!--
                        <xsl:variable name="datet" select="'2011-11-01T12:13:59'"/>
                        -->
                        <xsl:variable name="finaldate" select="format-dateTime($datet, '[D01] [MN,*-3]. [Y0001] [H01]:[m01]', 'fr', (), ())"/>
                        <xsl:value-of select="$finaldate" />

                        <!-- 
                        <xsl:value-of select="$dtime" /> 
                        -->
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我用Firefox打开XML ...

2 个答案:

答案 0 :(得分:1)

  

称为未知的XPath扩展函数

这听起来像是在试图将XPath 2.0表达式与只能支持XSLT 1.0的处理器一起使用。要使用dateTime函数,您需要一个2.0处理器,如Saxon或Altova XML。

答案 1 :(得分:0)

为什么不自行格式化日期?这并不难 - 即使使用XSLT 1.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="/">
<html>
<body>
<xsl:for-each select="AllMessages/messages">
    <div id="date">
    <xsl:call-template name="formatDate">
        <xsl:with-param name="date" select="@date"/>
    </xsl:call-template>
    </div>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template name="formatDate">
    <xsl:param name="date"/>
    <xsl:param name="y" select="substring($date, 1, 4 )"/>
    <xsl:param name="m" select="substring($date, 6, 2 )"/>
    <xsl:param name="d" select="substring($date, 9, 3 )"/>
    <xsl:param name="mmm" select="substring('JanFebMarAprMayJunJulAugSepOctNovDec', 3*($m -1)+1, 3 )"/>
    <xsl:param name="t" select="substring($date, 11, 6 )"/>
    <xsl:value-of select="concat($d, $mmm, '. ', $y, $t)" />
</xsl:template>

</xsl:stylesheet>

对于法语月份名称缩写,请使用:

<xsl:param name="mmm" select="normalize-space(substring('jan fév mar avr mai juinjuilaoûtseptoct nov déc', 4*($m -1)+1, 4 ))"/>