首页 > 解决方案 > 如何将此格式 mm/dd/yy 转换为 yyyy-mm-dd 并在 xslt 中添加 hh:mm:ss

问题描述

我想知道是否有人可以提供帮助。我一直在努力转换下面的日期。有没有办法将日期从 mm/dd/yy 转换为 yyyy-mm-dd 并在 xslt 中添加 hh:mm:ss?

这是我当前的输入 XML。我正在尝试转换“DateofService”。服务日期将始终为 mm/dd/yy,没有 hh:mm:ss。

                              <?xml version="1.0"?>
                             -<BCBSFMsg>
                               <BCBSFMsgHeader/>
                              -<BCBSFBody>
                               -<EIPMsg ImageType="PAPER"  MsgType="DocumentNotify">
                                       <ImageId>12344</ImageId>
                                       <FileExtension>TIF</FileExtension>
                                     <DateofService>11/27/18</DateofService>
                                                 <EIPMsg>
                                           </BCBSFMsg>

目前,这是我在 xslt 中的查询:

<xsl:variable name="Date">
        <xsl:variable name="sqlXref"> 
          SELECT CONVERT(CHAR(19),ai.ADDLINFO_INDEX_15, 101) + '00:00:00' AS 'ServiceFromDate' FROM   ADDITIONAL_INFORMATION ai with (nolock)  INNER JOIN IMAGES_DETAIL id with (nolock) WHERE id.IMAGE_ID = &apos;<xsl:value-of select="$IMAGE_ID"/>&apos;
         </xsl:variable>
        <xsl:value-of select="env:ExecuteScalar($provider, $connStr, $sqlXref)"/>
  </xsl:variable>

$Date在元素中传递变量

<xsl:element name ="DateofService">
<xsl:value-of select="$Date"/>
</xsl:element>  

请指教。

谢谢你。

标签: xslt

解决方案


如果您的输入 XML 包含:

<DateofService>11/27/18</DateofService>

然后是一个模板:

<xsl:template match="DateofService">
    <xsl:copy>
        <xsl:text>20</xsl:text>
        <xsl:value-of select="substring-after(substring-after(., '/'), '/')"/>
        <xsl:value-of select="format-number(substring-before(., '/'), '-00')"/>
        <xsl:value-of select="format-number(substring-before(substring-after(., '/'), '/'), '-00')"/>
        <xsl:text> 00:00:00</xsl:text>
    </xsl:copy>
</xsl:template>

将返回:

<DateofService>2018-11-27 00:00:00</DateofService>

请注意,这假设您的所有日期都在 21 世纪。


推荐阅读