在XSLT中格式化日期和时间。

时间:2022-06-10 20:08:05

I have the column which returns the date and time in the default format.

我有以默认格式返回日期和时间的列。

<xsl:value-of select="/CAudioFile/CRI/LocalStartTime"/>

Which returns

它返回

2014-05-08T08:01:26.4000000-0700

2014 - 05 - 08 - t08:01:26.4000000 - 0700

I want to put this time in this format:

我想把这段时间写成这样:

05/08/2014 08:01:26

05/08/2014 08:01:26

I have tried to get the format with the different combination of substring:

我试着用不同的子串组合得到格式:

<xsl:value-of select="substring(/CAudioFile/CRI/LocalStartTime,1,10)"/>

But it's either returning me just the date or just the time.

但它不是把日期还给我,就是把时间还给我。

Any suggestions?

有什么建议吗?

Thank you.

谢谢你!

3 个解决方案

#1


3  

You need to work a bit harder at it:

你需要更努力地去做:

<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 1, 4)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 12, 8)"/>

#2


2  

You didn't specify what version of XSLT. If you can use 2.0, you can use format-dateTime().

您没有指定XSLT的版本。如果可以使用2.0,则可以使用format-dateTime()。

In your case it's a little tricky though because your example isn't castable as an xs:dateTime because of the timezone. The timezone needs to be 2 digits; 07:00 instead of 0700. You can fix this by using replace() and casting as an xs:dateTime.

在您的示例中,这有点棘手,因为由于时区的原因,您的示例不能作为xs:dateTime。时区需要为2位数;07:00,而不是0700年。您可以通过使用replace()和强制转换作为xs:dateTime来解决这个问题。

Here's an example. I've put the replace() in an xsl:variable to make it easier to read. It isn't required though.

这是一个例子。我将replace()放在xsl:变量中,以便更容易阅读。虽然它不是必需的。

XML Input

XML输入

<test>2014-05-08T08:01:26.4000000-0700</test>

XSLT 2.0

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:variable name="dt" select="xs:dateTime(replace(normalize-space(.),'([+-]\d{2})(\d{2})$','$1:$2'))" as="xs:dateTime"/>
        <xsl:copy>
            <xsl:value-of select="format-dateTime($dt,'[M00]/[D00]/[Y] [H00]:[m00]:[s00]')"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Output

输出

<test>05/08/2014 08:01:26</test>

You can find more info on the picture string (second arg of format-dateTime()) here:

您可以在这里找到更多关于图片字符串(format-dateTime()的第二个arg)的信息:

http://www.w3.org/TR/xslt20/#date-picture-string

http://www.w3.org/TR/xslt20/ date-picture-string

#3


0  

These variables will give you the date (in ISO 8601 format) and the time using XPath 1.0 expressions:

这些变量将给出日期(ISO 8601格式)和使用XPath 1.0表达式的时间:

<xsl:variable name="date">
    <xsl:value-of select="substring-before(.,'T')"/>
</xsl:variable>
<xsl:variable name="time">
    <xsl:value-of select="substring-before(substring-after(.,'T'),'.')"/>
</xsl:variable>

To convert your date to the mm/dd/yyyy format, you can add this template to your stylesheet:

要将日期转换为mm/dd/yyyy格式,您可以将此模板添加到样式表中:

<xsl:template name="iso-to-mdy">
    <xsl:param name="iso-date"/>
    <xsl:variable name="year" select="substring($iso-date,1,4)"/>    
    <xsl:variable name="month" select="substring($iso-date,6,2)"/>
    <xsl:variable name="day" select="substring($iso-date,9,2)"/>
    <xsl:value-of select="$month"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$day"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$year"/>
</xsl:template>

And instead declare your date variable like this, passing your ISO 8601 date as a parameter:

然后像这样声明日期变量,将ISO 8601日期作为参数传递:

<xsl:variable name="date">
    <xsl:call-template name="iso-to-mdy">
        <xsl:with-param name="iso-date" select="substring-before(.,'T')"/>
    </xsl:call-template>
</xsl:variable>

Then you will have the date in the format you want. To print the space between time and date you can use <xsl:text>:

然后你将有你想要的格式的日期。要打印时间和日期之间的空间,可以使用 :

<date>
    <xsl:value-of select="$date"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$time"/>
</date>

which will print

这将打印

<date>05/08/2014 08:01:26</date>

See XSLT Fiddle

看到XSLT小提琴

#1


3  

You need to work a bit harder at it:

你需要更努力地去做:

<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 1, 4)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 12, 8)"/>

#2


2  

You didn't specify what version of XSLT. If you can use 2.0, you can use format-dateTime().

您没有指定XSLT的版本。如果可以使用2.0,则可以使用format-dateTime()。

In your case it's a little tricky though because your example isn't castable as an xs:dateTime because of the timezone. The timezone needs to be 2 digits; 07:00 instead of 0700. You can fix this by using replace() and casting as an xs:dateTime.

在您的示例中,这有点棘手,因为由于时区的原因,您的示例不能作为xs:dateTime。时区需要为2位数;07:00,而不是0700年。您可以通过使用replace()和强制转换作为xs:dateTime来解决这个问题。

Here's an example. I've put the replace() in an xsl:variable to make it easier to read. It isn't required though.

这是一个例子。我将replace()放在xsl:变量中,以便更容易阅读。虽然它不是必需的。

XML Input

XML输入

<test>2014-05-08T08:01:26.4000000-0700</test>

XSLT 2.0

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:variable name="dt" select="xs:dateTime(replace(normalize-space(.),'([+-]\d{2})(\d{2})$','$1:$2'))" as="xs:dateTime"/>
        <xsl:copy>
            <xsl:value-of select="format-dateTime($dt,'[M00]/[D00]/[Y] [H00]:[m00]:[s00]')"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Output

输出

<test>05/08/2014 08:01:26</test>

You can find more info on the picture string (second arg of format-dateTime()) here:

您可以在这里找到更多关于图片字符串(format-dateTime()的第二个arg)的信息:

http://www.w3.org/TR/xslt20/#date-picture-string

http://www.w3.org/TR/xslt20/ date-picture-string

#3


0  

These variables will give you the date (in ISO 8601 format) and the time using XPath 1.0 expressions:

这些变量将给出日期(ISO 8601格式)和使用XPath 1.0表达式的时间:

<xsl:variable name="date">
    <xsl:value-of select="substring-before(.,'T')"/>
</xsl:variable>
<xsl:variable name="time">
    <xsl:value-of select="substring-before(substring-after(.,'T'),'.')"/>
</xsl:variable>

To convert your date to the mm/dd/yyyy format, you can add this template to your stylesheet:

要将日期转换为mm/dd/yyyy格式,您可以将此模板添加到样式表中:

<xsl:template name="iso-to-mdy">
    <xsl:param name="iso-date"/>
    <xsl:variable name="year" select="substring($iso-date,1,4)"/>    
    <xsl:variable name="month" select="substring($iso-date,6,2)"/>
    <xsl:variable name="day" select="substring($iso-date,9,2)"/>
    <xsl:value-of select="$month"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$day"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$year"/>
</xsl:template>

And instead declare your date variable like this, passing your ISO 8601 date as a parameter:

然后像这样声明日期变量,将ISO 8601日期作为参数传递:

<xsl:variable name="date">
    <xsl:call-template name="iso-to-mdy">
        <xsl:with-param name="iso-date" select="substring-before(.,'T')"/>
    </xsl:call-template>
</xsl:variable>

Then you will have the date in the format you want. To print the space between time and date you can use <xsl:text>:

然后你将有你想要的格式的日期。要打印时间和日期之间的空间,可以使用 :

<date>
    <xsl:value-of select="$date"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$time"/>
</date>

which will print

这将打印

<date>05/08/2014 08:01:26</date>

See XSLT Fiddle

看到XSLT小提琴