xml – XSLT日期格式
发布时间:2020-12-16 07:56:52 所属栏目:百科 来源:网络整理
导读:我在这里看过各种建议,但没有一个真正有助于我的问题. 由于来自我的XML的来源,我可以通过以下三种格式接收日期; 04-04-2014(DD-MM-YYYY)04-Apr-2014(DD-MMM-YYYY)2014-04-04(YYYY-MM-DD) 我想有一个函数或简单的命令,将所有这些(除了第三个,但能够识别第三个
|
我在这里看过各种建议,但没有一个真正有助于我的问题.
由于来自我的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]')”/> 感谢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="^(
(dd)-(dd)-(dddd)
| (dd)-([A-Za-z]{{3}})-(dddd)
| (dddd)-(dd)-(dd)
| (dddd)-([A-Za-z]{{3}})-(dd)
| (dd)([A-Za-z]{{3}})(dddd)
| (dddd)([A-Za-z]{{3}})(dd))$">
<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:
<xsl:variable name="months" select="('Jan',...)" />
<xsl:analyze-string select="date" flags="x"
regex="^(
(dd)-(dd)-(dddd)
| (dd)-([A-Za-z]{{3}})-(dddd)
| (dddd)-(dd)-(dd))$">
<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>
每个示例格式都将匹配模式中三个备选项中的一个,并且对于非匹配备选项的所有regex-group调用将生成空字符串. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
