我在这里看过各种建议,但没有一个真正有助于我的问题.
由于来自我的XML的来源,我可以通过以下三种格式接收日期;
由于来自我的XML的来源,我可以通过以下三种格式接收日期;
04-04-2014(DD-MM-YYYY) 04-Apr-2014(DD-MMM-YYYY) 2014-04-04(YYYY-MM-DD)
我想有一个函数或简单的命令,将所有这些(除了第三个,但能够识别第三个是正确的)改为YYYY-MM-DD
我有一个很长的时间,当/何时/什么时候这样做,但必须有一个更简单的方法.我目前的XSLT做了以下事情;
<xsl:choose> <xsl:when test="contains(date,'Jan')"> <xsl:value-of select="concat(substring(date,6),'-01-',substring(date,1,2))" /> </xsl:when> <xsl:when test="contains(date,'Feb')"> <xsl:value-of select="concat(substring(date,'-02-','Mar')"> <xsl:value-of select="concat(substring(date,'-03-','Apr')"> <xsl:value-of select="concat(substring(date,'-04-','May')"> <xsl:value-of select="concat(substring(date,'-05-','Jun')"> <xsl:value-of select="concat(substring(date,'-06-','Jul')"> <xsl:value-of select="concat(substring(date,'-07-','Aug')"> <xsl:value-of select="concat(substring(date,'-08-','Sep')"> <xsl:value-of select="concat(substring(date,'-09-','Oct')"> <xsl:value-of select="concat(substring(date,'-10-','Nov')"> <xsl:value-of select="concat(substring(date,'-11-','Dec')"> <xsl:value-of select="concat(substring(date,'-12-',2))" /> </xsl:when> <xsl:when test="string-length($dateStart) = 2"> <xsl:value-of select="concat(substring(date,7),'-',4,2),2))" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="date"/> </xsl:otherwise> </xsl:choose>
因此,这将检查日期是否包含任何月份为JAN,2月等,然后如果没有,检查第一个数字是否 – 是2个字符(DD)和格式,否则它假定它已经是YYYY-MM-DD并按原样输出.
我试过 – < xsl:value-of select =“format-dateTime(date,'[Y0001] - [MN] - [D01]')”/>
但是这抱怨说日期年份不够长(因为日期被视为日期时间,应该采用YYYY-MM-DD格式)
感谢Ian roberts在下面的回答中,我创建了以下内容来处理更多场景并将输出分组在一起;
<xsl:variable name="months" select="('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')" /> <xsl:analyze-string select="date" flags="x" regex="^( (\d\d)-(\d\d)-(\d\d\d\d) | (\d\d)-([A-Za-z]{{3}})-(\d\d\d\d) | (\d\d\d\d)-(\d\d)-(\d\d) | (\d\d\d\d)-([A-Za-z]{{3}})-(\d\d) | (\d\d)([A-Za-z]{{3}})(\d\d\d\d) | (\d\d\d\d)([A-Za-z]{{3}})(\d\d))$"> <xsl:matching-substring> <xsl:value-of select="if (regex-group(4)) then concat(regex-group(4),regex-group(3),regex-group(2)) else ''"/> <xsl:value-of select="if (regex-group(7)) then concat(regex-group(7),format-number(index-of($months,regex-group(6)),'00'),regex-group(5)) else ''"/> <xsl:value-of select="if (regex-group(8)) then concat(regex-group(8),regex-group(9),regex-group(10)) else ''"/> <xsl:value-of select="if (regex-group(11)) then concat(regex-group(11),regex-group(12)),regex-group(13)) else ''"/> <xsl:value-of select="if (regex-group(16)) then concat(regex-group(16),regex-group(15)),regex-group(14)) else ''"/> <xsl:value-of select="if (regex-group(17)) then concat(regex-group(17),regex-group(18)),regex-group(19)) else ''"/> </xsl:matching-substring> </xsl:analyze-string>
由于您引用format-dateTime,因此您必须使用XSLT 2.0,因此可以使用正则表达式来处理它.如果您知道您将始终拥有这三种形式中的一种,那么您可以使用analyze-string:
原文链接:https://www.f2er.com/xml/292987.html<xsl:variable name="months" select="('Jan',...)" /> <xsl:analyze-string select="date" flags="x" regex="^( (\d\d)-(\d\d)-(\d\d\d\d) | (\d\d)-([A-Za-z]{{3}})-(\d\d\d\d) | (\d\d\d\d)-(\d\d)-(\d\d))$"> <xsl:matching-substring> <!-- year --> <xsl:value-of select="regex-group(4)"/> <xsl:value-of select="regex-group(7)"/> <xsl:value-of select="regex-group(8)"/> <xsl:text>-</xsl:text> <!-- month --> <xsl:value-of select="regex-group(3)"/> <xsl:value-of select="if (regex-group(6)) then format-number(index-of($months,'00') else ''"/> <xsl:value-of select="regex-group(9)"/> <xsl:text>-</xsl:text> <!-- day --> <xsl:value-of select="regex-group(2)"/> <xsl:value-of select="regex-group(5)"/> <xsl:value-of select="regex-group(10)"/> </xsl:matching-substring> </xsl:analyze-string>