【问题标题】:How to convert default mysql modtimes into UTC date time using xslt如何使用 xslt 将默认 mysql modtimes 转换为 UTC 日期时间
【发布时间】:2010-07-18 09:48:05
【问题描述】:

我需要使用 xslt 将默认的 mysql modtimes 转换为 UTC 格式的日期时间。

我有一个 xml 文档,其中包含源自 mysql modtimes 格式的日期: 2010-06-30 15:20:43.0

它们现在位于元素“日期戳”内的 xml 文档中 2010-03-16 13:52:56.0

为了通过 oai-pmh 提要发布,需要将它们转换为 UTC 格式: 2010-06-30T15:20:43Z

此处定义了 ISO 8601 UTC 日期: http://www.w3.org/TR/NOTE-datetime

我无法访问原始的 mysql 数据库,只有一个 xml 转储。

【问题讨论】:

  • 好问题 (+1)。请参阅我对简单 XSLT(和单行 XPath)解决方案的回答。 :)

标签: mysql xml xslt xpath


【解决方案1】:

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="text()">
  <xsl:value-of select=
   "concat(
           translate(substring-before(.,'.'),
                     ' ',
                     'T'
                     ),
            'Z'
            )
   "/>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时

<oai:datestamp xmlns:oai="some:ns">2010-03-16 13:52:56.0</oai:datestamp>

产生想要的正确结果

<oai:datestamp xmlns:oai="some:ns">2010-03-16T13:52:56Z</oai:datestamp>

注意:该值使用单个 XPath 表达式转换为所需格式,这就是我添加标签 xpath 的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2012-12-06
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多