在XSLT中将日期从名称转换为数字 [英] Converting Dates from Names to Numbers in XSLT
问题描述
我正在尝试将
11-APR-16格式的日期转换为xml,我想解析日期并将其转换为2016-04-11或yyyy-mm-dd
I am trying to convert a date in xml that is in the format 11-APR-16, I would like to parse the date and convert it in 2016-04-11 or yyyy-mm-dd
我当前正在使用Xslt代码转换日期:
I currently am using the Xslt code to convert the date:
<xsl:variable name="Date" select="//MEDIAITEMS_ROW/CREATED_DATE"/>
<xsl:variable name="Day" select="substring($Date,1,2)" />
<xsl:variable name="month" select="substring($Date,4,3)" />
<xsl:variable name="Year" select="substring($Date,8,2)" />
<xsl:template name="get-month-abbreviation">
<xsl:choose>
<xsl:when test="$month = JAN">01</xsl:when>
<xsl:when test="$month = FEB">02</xsl:when>
<xsl:when test="$month = MAR">03</xsl:when>
<xsl:when test="$month = APR">04</xsl:when>
<xsl:when test="$month = MAY">05</xsl:when>
<xsl:when test="$month = JUN">06</xsl:when>
<xsl:when test="$month = JUL">07</xsl:when>
<xsl:when test="$month = AUG">08</xsl:when>
<xsl:when test="$month = SEP">09</xsl:when>
<xsl:when test="$month = OCT">10</xsl:when>
<xsl:when test="$month = NOV">11</xsl:when>
<xsl:when test="$month = DEC">12</xsl:when>
<xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
但是我仍然返回值APR,如何将日期从字母转换为数字值
However I am still returning the value APR, how should I go about converting the date from letters to number values
推荐答案
因为您想将变量与字符串进行比较
更改您的 xsl:when
from例如:
Because you like to compare your variable with an string
change your xsl:when
from e.g.:
<xsl:when test="$month = JAN">01</xsl:when>
至:
<xsl:when test="$month = 'JAN'">01</xsl:when>
test = $ month = JAN
将变量 $ month
与当前节点中的元素 JAN
进行比较。
test="$month = JAN"
compares the variable $month
with an element JAN
in current node.
更新(仅)添加ARP的工作示例
Update add working example for ARP (only)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:variable name="Date" select="'11-APR-16'"/>
<xsl:variable name="Day" select="substring($Date,1,2)" />
<xsl:variable name="month" select="substring($Date,4,3)" />
<xsl:variable name="Year" select="substring($Date,8,2)" />
<xsl:template name="get-month-abbreviation">
<xsl:choose>
<xsl:when test="$month = 'APR'">04</xsl:when>
<xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="get-month-abbreviation" />
</xsl:template>
</xsl:styles
这篇关于在XSLT中将日期从名称转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!