string 如何在 xslt 2.0 中将字符串解析为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16851726/
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-09 01:56:56  来源:igfitidea点击:

How to parse string to date in xslt 2.0

stringdatexsltxslt-2.0

提问by VextoR

Is it possible to convert strings like 30042013(30 April 2013) to a date format?

是否可以将30042013(2013 年 4 月 30 日)之类的字符串转换为日期格式?

So I can use it later in functions like format-date

所以我以后可以在类似的功能中使用它 format-date

采纳答案by Tomalak

fn:dateTime($arg1 as xs:date?, $arg2 as xs:time?)will convert its arguments to xs:dateTime.

fn:dateTime($arg1 as xs:date?, $arg2 as xs:time?)将其参数转换为xs:dateTime.

Just use fn:substring()and fn:concat()to cut out the relevant parts and join them as yyyy-mm-ddbefore passing that to fn:dateTime.

只需使用fn:substring()fn:concat()切出相关部分并像yyyy-mm-dd之前一样将它们连接到fn:dateTime.

回答by Daniel Haley

Like Tomalak said, you can use substring()and concat()to build a string you can cast as an xs:date()(It doesn't sound like you want a dateTime.)

就像 Tomalak 所说的那样,您可以使用substring()concat()构建一个字符串,您可以将其转换为一个xs:date()(听起来不像您想要日期时间。)

Example:

例子:

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

    <xsl:variable name="in" select="'30042013'"/>

    <xsl:template match="/">
        <xsl:variable name="date" select="xs:date(concat(
            substring($in,5,4),'-',
            substring($in,3,2),'-',
            substring($in,1,2)))"/>
        <xsl:value-of select="format-date($date,'[MNn] [D], [Y]')"/>
    </xsl:template>

</xsl:stylesheet>

produces (with any XML input)

产生(使用任何 XML 输入)

April 30, 2013