【问题标题】:How to correctly use GMT on XSLT?如何在 XSLT 上正确使用 GMT?
【发布时间】:2019-11-30 00:48:29
【问题描述】:

这两个时间戳似乎在 XSLT 上使用了不同的 GMT。

Timestamp #1
-> 1559780505287
-> 5/6/2019, 9:21:45 PM GMT-3 (dd/mm/yyyy)

Timestamp #2
-> 1562681762005
-> 9/7/2019, 11:16:02 AM GMT-3 (dd/mm/yyyy)

使用在线 xslt 测试工具:https://xslttest.appspot.com/

转换后,时间戳 #1 为 6/6/2019,时间戳 #2 仍为 9/7/2019。 时间戳 #1 应该是 5/6/2019。

Transformation result

如何在 XSLT 上正确使用 GMT-3?

XML:

<java version="1.6.0_45" class="java.beans.XMLDecoder">
    <object class="com.MyApp">

        <void property="date1">
            <object class="java.util.Date">
                <long>1559780505287</long>
            </object>
        </void>

        <void property="date2">
            <object class="java.util.Date">
                <long>1562681762005</long>
            </object>
        </void>

    </object>
</java>

XSLT:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:decimal-format decimal-separator="," grouping-separator="."/>

<xsl:template match="/">
    <html>
        <head>
            <meta charset="utf-8"/>
            <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
            <meta name="viewport" content="width=device-width, initial-scale=1"/>
            <meta name="format-detection" content="telephone=no"/>

        </head>
        <body>
            <div class="container">

                <h1>Date 1</h1>
                <xsl:variable name="date1">
                    <xsl:value-of select="xs:date('1970-01-01') + java/object/void[@property='date1'] * xs:dayTimeDuration('PT0.001S')"/>
                </xsl:variable>

                <h2>
                    <xsl:value-of select="format-date($date1, '[D01]/[M01]/[Y0001]')"/>
                </h2>

                <h1>Date 2</h1>
                <xsl:variable name="date2">
                    <xsl:value-of select="xs:date('1970-01-01') + java/object/void[@property='date2'] * xs:dayTimeDuration('PT0.001S')"/>
                </xsl:variable>

                <h2>
                    <xsl:value-of select="format-date($date2, '[D01]/[M01]/[Y0001]')"/>
                </h2>

            </div>
        </body>
    </html>
</xsl:template>

【问题讨论】:

    标签: xslt xml-parsing


    【解决方案1】:

    您应该将纪元时间减去 GMT -3(3 小时 = 10800000 毫秒),因为您的转换是毫秒"PT0.001S"

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://www.w3.org/TR/xhtml1/strict">
    <xsl:decimal-format decimal-separator="," grouping-separator="."/>
    
    <xsl:template match="/">
        <html>
            <head>
                <meta charset="utf-8"/>
                <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
                <meta name="viewport" content="width=device-width, initial-scale=1"/>
                <meta name="format-detection" content="telephone=no"/>
    
            </head>
            <body>
                <div class="container">
    
                    <h1>Date 1</h1>
                    <xsl:variable name="date1">
    
                         <xsl:value-of select="xs:date('1970-01-01') + (java/object/void[@property='date1']-10800000) * xs:dayTimeDuration('PT0.001S')"/>
                    </xsl:variable>
                    <h2>
                        <xsl:value-of select="format-date($date1, '[D01]/[M01]/[Y0001]')"/>
                    </h2>
    
                    <h1>Date 2</h1>
                    <xsl:variable name="date2">
                        <xsl:value-of select="xs:date('1970-01-01') + (java/object/void[@property='date2']-10800000) * xs:dayTimeDuration('PT0.001S')"/>
                    </xsl:variable>
    
                    <h2>
                        <xsl:value-of select="format-date($date2, '[D01]/[M01]/[Y0001]')"/>
                    </h2>
    
                </div>
            </body>
        </html>
    </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 这成功了!奇怪...我减去 xs:dayTimeDuration('PT3H') 而不是 10800000 并且时间戳 #2 已转换为 8/7/2019。 &lt;xsl:value-of select="xs:date('1970-01-01') + java/object/void[@property='date2'] * xs:dayTimeDuration('PT0.001S') - xs:dayTimeDuration('PT3H')"/&gt;
    • pt3h 在不同的时区
    【解决方案2】:

    我建议您将时间戳转换为 dateTime,将其调整为您的时区,然后对其进行格式化:

    XSLT 2.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">
    
    <xsl:template match="/java">
        <html>
            <body>
                <xsl:for-each select="object/void">
                    <xsl:variable name="dateTime-GMT" select="xs:dateTime('1970-01-01T00:00:00Z') + object/long * xs:dayTimeDuration('PT0.001S')"/> 
                    <xsl:variable name="dateTime-adjusted" select="adjust-dateTime-to-timezone($dateTime-GMT, xs:dayTimeDuration('-PT3H'))"/>
                    <h1>
                        <xsl:value-of select="@property"/>
                    </h1>
                    <h2>
                        <xsl:value-of select="format-dateTime($dateTime-adjusted, '[D01]/[M01]/[Y0001] [h]:[m01]:[s01] [P] [z]')"/>
                    </h2>
                </xsl:for-each>
             </body>
        </html>
    </xsl:template>
    
    </xsl:stylesheet>
    

    结果

    <html>
       <body>
          <h1>date1</h1>
          <h2>05/06/2019 9:21:45 p.m. GMT-03:00</h2>
          <h1>date2</h1>
          <h2>09/07/2019 11:16:02 a.m. GMT-03:00</h2>
       </body>
    </html>
    

    渲染

    【讨论】:

    • 输出只需要日期。但这个公认的解决方案包括时间。我不赞成。
    • 我已将格式调整为不包含时间:&lt;xsl:value-of select="format-dateTime($dateTime-adjusted, '[D01]/[M01]/[Y0001]')"/&gt;
    • @EdBangga 实际上,请求的输出声明为-&gt; 5/6/2019, 9:21:45 PM GMT-3。但是,如果您认为此答案没有用,请随意投票。
    猜你喜欢
    • 2022-07-08
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多