xml 在 xslt 中格式化和显示日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7805613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:59:32  来源:igfitidea点击:

format and display datetime in xslt

xmlxsltxpath

提问by Naor

I have an XML with datetime data:

我有一个带有日期时间数据的 XML:

<A>
    <StartDate>2011-11-01T00:00:00</StartDate>
    <EndDate>2011-11-30T00:00:00</EndDate>
    <IsRecurring>false</IsRecurring>
</A>

I need to get in xslt only the dates in the following format:

我只需要在 xslt 中输入以下格式的日期:

01/11/2011 - 30/11/2011

When I do:

当我做:

<xsl:value-of select="A/StartDate/"> - <xsl:value-of select="A/EndDate/">

I get this:

我明白了:

2011-11-01T00:00:00 - 2011-11-30T00:00:00

How can I display it properly?

我怎样才能正确显示它?

回答by Kirill Polishchuk

Look at XPath string functions: substring, substring-beforeand etc.

看看 XPath 字符串函数:substringsubstring-before等等。

Reference: http://www.w3.org/TR/xpath/#section-String-Functions

参考:http: //www.w3.org/TR/xpath/#section-String-Functions

<xsl:variable name="dt" select="'2011-11-01T12:13:59'"/>

        <xsl:value-of select="concat(
                      substring($dt, 9, 2),
                      '/',
                      substring($dt, 6, 2),
                      '/',
                      substring($dt, 1, 4)
                      )"/>

回答by JBert

EDIT: If you still have to use XSLT 1.x, you could look at EXSLT's date:format-date user function. In that case the pattern is dd/MM/yyyy.

编辑:如果您仍然必须使用 XSLT 1.x,您可以查看EXSLT 的 date:format-date user function。在这种情况下,模式是dd/MM/yyyy

If your are using XSLT 2.0, it is handier to use the format-dateTimefunction.

如果您使用的是 XSLT 2.0,则使用该format-dateTime功能会更方便。

Here's your example in XSLT 2.0:

这是您在 XSLT 2.0 中的示例:

<xsl:value-of select="format-dateTime(A/StartDate, '[D01]/[M01]/[Y0001]')" /> 
- <xsl:value-of select="format-dateTime(A/EndDate, '[D01]/[M01]/[Y0001]')" />

回答by FailedDev

In addition to @Kirill Polishchuk answer if you were using XSLT 2.0 you can simply do a replace :

除了@Kirill Polishchuk 回答之外,如果您使用的是 XSLT 2.0,您还可以简单地进行替换:

substring(replace($input, "(\d{4})-(\d{2})-(\d{2})", "//"), 0, 11)

Where $inputis the contents of your nodes which contain the dates.

$input包含日期的节点的内容在哪里。