我需要使用 xslt 将默认的 mysql modtimes 转换为 UTC 格式的日期时间。
我有一个 xml 文档,其中包含源自 mysql modtimes 的日期,格式如下: 2010-06-30 15:20:43.0
它们现在位于元素“datestamp”内的 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 转储。
最佳答案
这个转换:
<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
的原因。
关于mysql - 如何使用 xslt 将默认的 mysql modtimes 转换为 UTC 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3273838/