【问题标题】:XSLT - convert Date from any format into a specific formatXSLT - 将日期从任何格式转换为特定格式
【发布时间】:2019-01-24 04:43:51
【问题描述】:

我在这个 XML 文件的字段中有一个日期值:

<xsl:value-of select="nightlyRate/@date" />

我想把它转换成给定的格式:

<xsl:param name="dateformat" select="yyyy/mm/dd">

如何在我的 XSL 转换中做到这一点?

【问题讨论】:

  • 无法将“任何格式的日期”转换为已知格式。您必须提前知道格式。如果输入是“YYYY-MM-DD”并且您使用的是 XSLT 2.0,那么您可以使用 format-date() 函数对其进行格式化。否则你需要使用字符串函数来操作它。

标签: xml xslt


【解决方案1】:

除非你有一些聪明的方法知道 03/04/1920 是代表 3 月 4 日还是 4 月 3 日,否则这个问题是不可能的。

在开始使用 XSLT 或任何其他语言编写代码之前,您需要明确说明代码应该做什么。

【讨论】:

    【解决方案2】:

    XSLT 1.0 中,没有将日期转换为特定格式的函数(如 @michael.hor257k 所说)。

    您可以根据自己的环境具体通过以下两种方式实现:

    1. 如果您使用的是 java,您可以使用 java.util.TimeZonejava.util.Calendar API 编写 Util 类.

    在 util 类中定义最终静态常量,并使用这些 API 将值解析为所需的日期格式。

    2.假设您有如下输入源:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Root>
        <nightlyRate date="20180312" />
    </Root>
    

    然后 xslt 对其进行转换将使用简单的substring() 函数:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">
    
    <xsl:output indent="yes" />
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/">
        <date>
            <xsl:call-template name="getFormattedDate">
                <xsl:with-param name="date" select="Root/nightlyRate/@date" />
            </xsl:call-template>
        </date>
    </xsl:template>
    
    <xsl:template name="getFormattedDate">
        <xsl:param name="date" />
        <xsl:value-of select="concat(substring($date,1,4),'/',substring($date,5,2),'/',substring($date,7,2))" />
    </xsl:template>
    

    XSLT 2.0 中,

    取同一个输入源,可以写成:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output omit-xml-declaration="yes" />
    
    <xsl:template match="/">
        <xsl:variable name="dateString">
            <xsl:value-of select="substring(Root/nightlyRate/@date,1,4)" />
            <xsl:text>-</xsl:text>
            <xsl:value-of select="substring(Root/nightlyRate/@date,5,2)" />
            <xsl:text>-</xsl:text>
            <xsl:value-of select="substring(Root/nightlyRate/@date,7,2)" />
        </xsl:variable>
        <RequiredFormat>
            <xsl:value-of select="format-date(xs:date($dateString), '[Y]/[M]/[D]')" />
        </RequiredFormat>
        <OtherFormat>
            <xsl:value-of select="format-date(xs:date($dateString), '[MNn] [D], [Y]')" />
        </OtherFormat>
    </xsl:template>
    

    输出将是:

    <RequiredFormat>2018/3/12</RequiredFormat>
    <OtherFormat>March 12, 2018</OtherFormat>
    

    【讨论】:

    • @michael.hor257k :我的错。由于我自己的项目,我习惯使用它。这里没有必要使用它。谢谢。
    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多