使用 xslt 格式化日期值

Formatting Date Value Using xslt

如何在 XSLT version1.0 中格式化以下 XML 日期元素? GenerationTime 必须在输出 XML.

中以模式 yyyy-MM-dd'T'HH:mm:ss 格式化
<Values>
  <value name="GenerationTime">Tue Dec 29 14:32:53 UTC 2020</value>
<Values>

这是我从我的产品 (Intergation Server) 文档中找到的内容

When executed, the XSLT service calls an external XSLT engine to convert the XML data. The external XSLT engine must be Java API for XML Processing (JAXP)-compatible. By default, Integration Server includes the Xerces parser and the Xalan XSLT style sheet processor from the Apache Software Foundation.

Xalan 支持 EXSLT str:tokenize() 扩展函数,因此您可以执行以下操作:

<xsl:template name="reformat-date">
    <xsl:param name="input"/>
    <!-- extract dateTime components -->
    <xsl:variable name="tokens" select="str:tokenize($input, ' ')"/>
    <xsl:variable name="mmm" select="$tokens[2]"/>
    <xsl:variable name="day" select="$tokens[3]"/>
    <xsl:variable name="time" select="$tokens[4]"/>
    <xsl:variable name="year" select="$tokens[6]"/>
    <!-- convert MMM to month number -->
    <xsl:variable name="month" select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', $mmm)) div 3 + 1" />
    <!-- output -->
    <xsl:value-of select="$year"/>
    <xsl:value-of select="format-number($month, '-00')"/>
    <xsl:value-of select="format-number($day, '-00')"/>
    <xsl:text>T</xsl:text>
    <xsl:value-of select="$time"/>
</xsl:template>

演示http://xsltransform.net/6qCcdd5