【问题标题】:XSLT transformation string to datetime formatXSLT 将字符串转换为日期时间格式
【发布时间】:2013-08-08 16:22:01
【问题描述】:

我正在尝试将字符串格式的日期/时间转换为日期时间格式。我目前有这个,但它不工作:

<xsl:function name="hcim:ParseDateTime" as="xs:dateTime">
    <xsl:param name="DateTimeAsString" as="xs:string"/>

    <xsl:variable name="date" select="xs:date(substring($DateTimeAsString, 0, 10))"/>
    <xsl:variable name="time" select="xs:time(substring($DateTimeAsString, 11, 8))"/>
    <xsl:value-of select="dateTime($date, $time)" />
</xsl:function>

我得到的错误是:

XSLT2 Transformation failed: Error in XPath 2.0 expression 
    (Cast failed, invalid lexical value - xs:date 'String' - xs:date)
Error occurred in file '******' in statement 
    'select="xs:date(substring($DateTimeAsString, 0, 10))"'.

有没有更好的方法来做到这一点?我传入的字符串格式为:

2011-07-15 01:05:14 PM

【问题讨论】:

    标签: date datetime xslt time


    【解决方案1】:

    对于xs:date 部分,我认为&lt;xsl:variable name="date" select="xs:date(substring($DateTimeAsString, 1, 10))"/&gt; 应该可以工作。对于xs:time 部分,我认为您需要比提取子字符串更多的工作,因为您的样本有PM,因此您需要将其转换为24 小时制。要在函数中返回某种类型的值,请使用&lt;xsl:sequence select="..."/&gt;,而不是&lt;xsl:value-of select="..."/&gt;

    我已阅读 http://en.wikipedia.org/wiki/12_hour_clock 并尝试实现:

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:mf="http://example.com/mf"
        exclude-result-prefixes="xs mf">
    
    
    <xsl:function name="mf:parseDateTime" as="xs:dateTime">
      <xsl:param name="input" as="xs:string"/>
      <xsl:variable name="date" as="xs:date" select="xs:date(substring($input, 1, 10))"/>
      <xsl:variable name="clock" as="xs:string" select="substring($input, 12, 8)"/>
      <xsl:variable name="hours" as="xs:integer" select="xs:integer(substring($clock, 1, 2))"/>
      <xsl:variable name="suffix" as="xs:string" select="substring($input, 21)"/>
      <xsl:message select="concat('Suffix: |', $suffix , '|')"/>
      <xsl:variable name="time" as="xs:time"
        select="if ($suffix eq 'AM') 
                then (if ($hours eq 12) 
                      then xs:time($clock) - xs:dayTimeDuration('PT12H')
                      else xs:time($clock))
                else (if ($hours eq 12)
                      then xs:time($clock)  
                      else xs:time($clock) + xs:dayTimeDuration('PT12H'))"/>
      <xsl:sequence select="dateTime($date, $time)"/>
    </xsl:function>
    
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="value">
      <xsl:copy>
        <xsl:value-of select="mf:parseDateTime(.)"/>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    给定输入

    <values>
      <value>2013-08-08 12:00:00 AM</value>
      <value>2013-08-08 12:01:00 AM</value>
      <value>2013-08-08 12:59:00 AM</value>
      <value>2013-08-08 01:00:00 AM</value>
      <value>2013-08-08 11:00:00 AM</value>
      <value>2013-08-08 11:59:00 AM</value>
      <value>2013-08-08 12:00:00 PM</value>
      <value>2013-08-08 12:01:00 PM</value>
      <value>2013-08-08 12:59:00 PM</value>
      <value>2013-08-08 01:00:00 PM</value>
      <value>2013-08-08 11:00:00 PM</value>
      <value>2013-08-08 11:59:00 PM</value>
    </values>
    

    Saxon 9.5 将其转换为

    <values>
      <value>2013-08-08T00:00:00</value>
      <value>2013-08-08T00:01:00</value>
      <value>2013-08-08T00:59:00</value>
      <value>2013-08-08T01:00:00</value>
      <value>2013-08-08T11:00:00</value>
      <value>2013-08-08T11:59:00</value>
      <value>2013-08-08T12:00:00</value>
      <value>2013-08-08T12:01:00</value>
      <value>2013-08-08T12:59:00</value>
      <value>2013-08-08T13:00:00</value>
      <value>2013-08-08T23:00:00</value>
      <value>2013-08-08T23:59:00</value>
    </values>
    

    该函数可能应该明确检查PM 的存在,而不是假设与AM 不相等意味着它是PM 时间。

    【讨论】:

    • 是的,substring($DateTimeAsString, 0, 10) 在这种情况下会产生 2011-07-1,因为第 0 位没有字符。
    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多